perform action if date change

人走茶凉 提交于 2019-12-10 11:56:10

问题


i need to perform some action if date change. Means at the time of app launch it check the today date, and if today date is change from last 24 hour time,then it will perform some action. Is it possible as we don't need to run background thread. I just want to add some sort of condition in delegate method.

like : if on app launch it first save today date and save that date. After again login it will compare that date with current date and if fount its 24 hour change date then it will perform some operation. how i do that?xc

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


回答1:


You can use NSUserDefaults to save the date.

The following code explicitly checks whether the difference between the last and current app launch is more than or equal to 24 hours. It then saves the current date in the userDefaults.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoginTime"] != nil)
    {
        NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoginTime"];
        NSDate *currentDate = [NSDate date];

        NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:lastDate];
        double secondsInAnHour = 3600;
        NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;

        if (hoursBetweenDates >= 24)
        {
            //Perform operation here.
        }
    }

    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastLoginTime"];//Store current date

    return YES;
}



回答2:


Add below line of code in didFinishLaunchingWithOptions method

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

Implement method in YourApplicationDelegate.m file

-(void)timeChange
{
  //Do necessary work here
}

EDIT : Mixing @ZeMoon's answer this would work perfect so changes in timeChange method

-(void)timeChange
{
  if ([[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoginTime"] != nil)
  {
    NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoginTime"];
    NSDate *currentDate = [NSDate date];

    NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:lastDate];
    double secondsInAnHour = 3600;
    NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;

    if (hoursBetweenDates >= 24)
    {
        //Perform operation here.
    }
  }

  [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastLoginTime"];//Store current date
}


来源:https://stackoverflow.com/questions/26628020/perform-action-if-date-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!