在AppDelegate.m 文件中
先声明一个存放时间的类型;
@property(nonatomic,strong)NSDate * backgroundDate;
再获取到进入后台的时间戳,并将它复制给 backgroundDate
- (void)applicationDidEnterBackground:(UIApplication *)application {
self.backgroundDate = [NSDate date];
}
当程序进入到激活状态时再次获取当前时间,进行对比
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSDate * currentDate = [NSDate date];
NSTimeInterval start = [self.backgroundDate timeIntervalSince1970]*1;
NSTimeInterval end = [currentDate timeIntervalSince1970]*1;
NSTimeInterval value = end - start;
NSLog(@"时间差%f",value);
}
个人 demo: 挂起时间超过10s 会跳转到输入密码界面 输入正确才能继续操作
Demo