如果日期更改,则执行操作 [英] perform action if date change

查看:114
本文介绍了如果日期更改,则执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果日期更改,我需要执行一些操作。
表示在应用启动时检查今天的日期,如果今天的日期与过去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;
}

这篇关于如果日期更改,则执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆