添加以下代码行didFinishLaunchingWithOptions
方法
//for new date change
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timeChange) name:UIApplicationSignificantTimeChangeNotification object:nil];
实现方法YourApplicationDelegate.m
文件
-(void)timeChange
{
//Do necessary work here
}
编辑:混合@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
}
谢谢Zemoon,快速回答你的代码似乎适合我。 .... – iphonemaclover 2014-10-29 10:47:16
这将工作,但当应用程序是开放的和新的日期变化,那么这将无法正常工作。 – 2014-10-29 10:48:29
OP特别想要在应用程序启动时检查24小时的时间间隔。该代码完全符合该要求。 – ZeMoon 2014-10-29 10:50:11