简体   繁体   中英

How to fire action on date change?

I have Sqlite database.

I want to insert row in database when date changes.

I just know that i should use UILocalNotification ,But how i don't know.

other way is to run NSTimer in background but i don't want to do that.

from tutorial i tried:

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];
 }

but in my app it should insert in database either app is in background or foreground. insertion should place when date changes

you can store [NSDate date]; to some variable and put condition according to last date and change that variable inside that condition with today's date with the use of [NSDate date]; and do modifications as you want

If you want to go with UILocalNotification then see below information.

Set fireDate of UILocalNotification as you need and your notification will generated on this date. And you will get generated notification at 2 (Two) ways. such like,

1) in didFinishLaunchingWithOptions

=> it is helpful if your application is not running;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   .
   .
   .
   UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
   if (localNotif) 
   {
      /// do your stuff
   }
   .
   .
}

2) use delegate method of UILocalNotification

=> it is helpful if your application is running;

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    /// do your stuff
}

You can write your insert data here;

Add this code in didFinishLaunchingWithOptions method

// date change
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dateChange) name:UIApplicationSignificantTimeChangeNotification object:nil];

Implement method in YourDelegate.m file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM