当我将应用程序置于后台模式时,我在 NSUserDefaults
中保存了一个值,然后当应用程序再次激活时,该值就不同了。
我保存值:
- (void)appDidEnterBackground:(NSNotification *)notification {
//Tiempo inicial de inactividad
NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults];
NSTimeInterval timestamp = ([[NSDate date] timeIntervalSince1970] * 1000);
[dispositivo setFloat:timestamp forKey:@"StartBackground"];
NSLog(@"Start background: %f", timestamp);
[[NSUserDefaults standardUserDefaults] synchronize];
}
日志:启动后台:1418731653366.276123
我要恢复值(value):
- (void)appDidBecomeActive:(NSNotification *)notification {
NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults];
NSTimeInterval startDate = [dispositivo floatForKey:@"StartBackground"];
NSLog(@"Start date: %f", startDate);
}
日志:开始日期:1418731716608.000000
这是我唯一使用这个值的地方。 感谢您的提前。
最佳答案
NSTimeInterval 双击它,你会得到
typedef double NSTimeInterval;
所以你应该将值保存在 double 而不是 float
[dispositivo setDouble:timestamp forKey:@"StartBackground"];
获取数据
NSTimeInterval startDate = [dispositivo doubleForKey:@"StartBackground"];
最好保存 NSDate
对象 :)
关于ios - 返回后台时 NSUserDefaults 不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27504503/