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

一緒に行きたい場合はUILocalNotification、以下の情報を参照してください。

必要に応じて設定するfireDateと、この日付に通知が生成されます。UILocalNotificationそして、生成された通知を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 に答える
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 に答える