如果日期更改,我需要执行一些操作。 表示在应用启动时检查今天的日期,如果今天的日期与过去24小时的时间相比有所更改,则它将执行某些操作。 是否可以,因为我们不需要运行后台线程。 我只想在委托方法中添加某种条件。
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:如果在应用程序启动时首先保存今天并保存日期。 再次登录后,它将把该日期与当前日期进行比较,如果发现其24小时更改日期,它将执行一些操作。 xc
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
推荐答案
您可以使用NSUserDefaults保存日期
You can use NSUserDefaults to save the date.
以下代码明确检查上一次和当前启动应用程序之间的时间差是否大于或等于24小时。然后将当前日期保存在userDefaults中。
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; }更多推荐
如果日期更改,则执行操作
发布评论