Iphone 如何在日期更改时启动操作?
我有Sqlite数据库 我想在日期更改时在数据库中插入行 我只知道我应该使用Iphone 如何在日期更改时启动操作?,iphone,ios,objective-c,sqlite,uilocalnotification,Iphone,Ios,Objective C,Sqlite,Uilocalnotification,我有Sqlite数据库 我想在日期更改时在数据库中插入行 我只知道我应该使用UILocalNotification,但我不知道如何使用 另一种方法是在后台运行NSTimer,但我不想这样做 从教程中,我尝试了: UIApplication* app = [UIApplication sharedApplication]; NSDate *date = [NSDate date]; // create the notification and then set it's parameters U
UILocalNotification
,但我不知道如何使用
另一种方法是在后台运行NSTimer,但我不想这样做
从教程中,我尝试了:
UIApplication* app = [UIApplication sharedApplication];
NSDate *date = [NSDate date];
// create the notification and then set it's parameters
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification)
{
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
notification.alertBody = @"DONE:";
// this will schedule the notification to fire at the fire date
[app scheduleLocalNotification:notification];
// this will fire the notification right away, it will still also fire at the date we set
[app presentLocalNotificationNow:notification];
}
但在我的应用程序中,它应该在数据库中插入应用程序在后台或前台。当日期更改时应插入,您可以存储[NSDate date];根据上一个日期设置条件,并使用[NSDate date]使用今天的日期将该变量更改为该条件内的变量;如果要使用
UILocalNotification
,请查看下面的信息
根据需要设置UILocalNotification
的fireDate
,您的通知将在此日期生成。
您将通过两种方式获得生成的通知。例如
1)在中使用选项完成启动
=>如果您的应用程序未运行,这将非常有用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.
.
.
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
/// do your stuff
}
.
.
}
2)使用UILocalNotification的委托方法
=>如果您的应用程序正在运行,则会很有帮助
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
/// do your stuff
}
您可以在此处写入插入数据 在didfishlaunchingwithoptions
方法中添加此代码
// date change
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dateChange) name:UIApplicationSignificantTimeChangeNotification object:nil];
在YourDelegate.m
文件中实现方法
-(void)dateChange
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"LastAppOpenTime"] != nil)
{
NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastAppOpenTime"];
NSDate *currentDate = [NSDate date];
NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:lastDate];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
if (hoursBetweenDates >= 24)
{
//update database here.
}
}
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastAppOpenTime"];//Store current date
}
为什么需要在日期更改的准确时间添加此行?如果你的应用程序在新的一天启动,但行尚未添加,为什么不简单地添加新行呢?NSTimer在后台只能工作10分钟,最好根据你的要求使用UILocalNotification。否,我想在日期更改时插入。。若我照你们说的做了,那个么在早上10点,我必须向用户发送通知。。这两个应用程序都在后台或前台。谢谢@AnandGautam,但是怎么做呢?我正在获取日期,但我想在午夜日期更改时在数据库中插入行…哦,我明白了,然后你需要为其设置localPushNotification,并带有存储的日期和更改的日期。如果你的应用程序涉及到php Web服务,那么你可以在服务器端因为在服务器端更容易考虑日期的变化我的应用完全基于时钟和通知,不涉及网络服务。。。你有任何链接,我可以参考…你可以尝试这两个链接1)和2)不要忘记向上投票,如果它有助于你:)是的,我知道。。。但我应该在哪里写火灾通知,这样通知就可以根据时钟触发,而不依赖于任何点击或应用程序的后台前台..当日期更改时。。。午夜12:00在数据库中插入行。。上午11:00更新数据库中的行,下午6:00更新数据库中的行。希望现在清楚了。使用通知是个坏主意,但您可以在DidFinishLaunching中创建3个通知,使用不同的fireDate选项,例如12:00、11:00 am和6:00 pm,我希望您知道您需要做什么:)然后,我应该使用什么来代替通知?不可能触发此类通知,如果您大约5天没有打开应用程序,那么您如何在数据库中更新/插入这5天的数据??