9to*_*ios 0 xcode objective-c datetime-format ios
如果日期改变,我需要执行一些操作.在应用程序启动时的意思是检查今天的日期,如果今天的日期是从最后24小时的时间改变,那么它将执行一些操作.是否可能因为我们不需要运行后台线程.我只想在委托方法中添加某种条件.
喜欢:如果在应用程序启动它首先保存今天日期并保存该日期.再次登录后,它会将该日期与当前日期进行比较,如果将其更改为24小时更改日期,则会执行某些操作.我怎么做的?xc
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
在didFinishLaunchingWithOptions方法中添加以下代码行
//for new date change
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timeChange) name:UIApplicationSignificantTimeChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
在YourApplicationDelegate.m文件中实现方法
-(void)timeChange
{
//Do necessary work here
}
Run Code Online (Sandbox Code Playgroud)
编辑:混合@ZeMoon's答案,这将完美,所以timeChange方法的变化
-(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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |