0

我有 SQLite 数据库。

我想在日期更改时在数据库中插入行。

我只知道我应该使用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];
 }

但是在我的应用程序中,它应该在数据库中插入应用程序在后台或前台。插入应在日期更改时进行。

4

3 回答 3

0

didFinishLaunchingWithOptions在方法中添加此代码

// 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
}
于 2014-12-20T11:21:46.273 回答
0

如果您想一起去,UILocalNotification请参阅以下信息。

根据您的需要设置fireDateUILocalNotification您的通知将在此日期生成。您将以2(两种)方式获得生成的通知。诸如此类,

1)didFinishLaunchingWithOptions

=> 如果您的应用程序未运行,这将很有帮助;

- (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
}

你可以在这里写你的插入数据;

于 2013-09-06T06:28:07.997 回答
0

您可以存储 [NSDate 日期];到某个变量并根据最后日期放置条件,并使用 [NSDate date] 将条件内的变量更改为今天的日期;并根据需要进行修改。

于 2013-09-06T06:16:30.157 回答