我有和应用程序间隔给出数据使用。我想在使用数据使用之前检测设备是否已重新启动。我尝试使用mach_absolute_time()但我什么都听不懂。有可能吗?
答案 0 :(得分:1)
您可以像这样获得系统正常运行时间:
let systemUptime = NSProcessInfo.processInfo().systemUptime; // swift
NSTimeInterval timeInterval = [NSProcessInfo processInfo].systemUptime; //Objective c
您可以将其存储在NSUserDefaults中,并使用它来确定是否重新启动。
请查看this帖子以获取更多信息。
斯威夫特3:
let systemUptime: TimeInterval = ProcessInfo.processInfo.systemUptime
答案 1 :(得分:-1)
这是我做的一个。需要使用GMT中的当前时间以及自上次重启以来的时间来推断设备上次重启的日期。然后,它使用NSUserDefaults跟踪此日期在内存中。享受吧!
#define nowInSeconds CFAbsoluteTimeGetCurrent()//since Jan 1 2001 00:00:00 GMT
#define secondsSinceDeviceRestart ((int)round([[NSProcessInfo processInfo] systemUptime]))
#define storage [NSUserDefaults standardUserDefaults]
#define DISTANCE(__valueOne, __valueTwo) ((((__valueOne)-(__valueTwo))>=0)?((__valueOne)-(__valueTwo)):((__valueTwo)-(__valueOne)))
+(BOOL)didDeviceReset {
static BOOL didDeviceReset;
static dispatch_once_t onceToken;
int currentRestartDate = nowInSeconds-secondsSinceDeviceRestart;
int previousRestartDate = (int)[((NSNumber *)[storage objectForKey:@"previousRestartDate"]) integerValue];
int dateVarianceThreshold = 10;
dispatch_once(&onceToken, ^{
if (!previousRestartDate || DISTANCE(currentRestartDate, previousRestartDate) > dateVarianceThreshold) {
didDeviceReset = YES;
} else {
didDeviceReset = NO;
}
});
[storage setObject:@(currentRestartDate) forKey:@"previousRestartDate"];
[storage synchronize];
return didDeviceReset;
}