在您的 appdelegate.m 文件中使用以下代码
把下面的方法
-(void)minCalculation_backgroundtime:(NSString *)backgroundTime forgroundTime:(NSString *)foreGroundTime
{
NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
[dateformat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
NSDate *lastDate = [dateformat dateFromString:foreGroundTime];
NSDate *todaysDate = [dateformat dateFromString:backgroundTime];
NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
NSTimeInterval dateDiff = lastDiff - todaysDiff;
int min = dateDiff/60;
NSLog(@"Good to see you after %i minutes",min);
}
并替换以下两种方法
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
NSString *backGroundTime = [dateFormat stringFromDate:[NSDate date]];
[[NSUserDefaults standardUserDefaults]setValue:backGroundTime forKey:@"backGroundTime"];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
NSString *foreGroundTime = [dateFormat stringFromDate:[NSDate date]];
NSString *backGroundTime = [[NSUserDefaults standardUserDefaults]valueForKey:@"backGroundTime"];
[self minCalculation_backgroundtime:backGroundTime forgroundTime:foreGroundTime];
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
试试这个你的问题会解决