6

看看人们为此想出了什么。基本上我想知道自我的应用程序上次启动以来实际设备是否已重新启动。人们使用什么方法来发现这一点?(如果有的话?)

我考虑过使用 mach_absolute_time 但这仍然是一种不可靠的方法。

干杯

4

2 回答 2

5

不确定这是否是您想要的,但请看一下:

https://github.com/pfeilbr/ios-system-uptime

在这个例子中,作者从内核任务进程中获取它。

或者你可以看看 mach_absolute_time 路线,有一个类似的目标苹果官方问答https://developer.apple.com/library/mac/#qa/qa1398/_index.html

希望这可以帮助。

于 2012-10-30T00:59:46.040 回答
0

这是我做的一个。它需要 GMT 的当前时间和自上次重新启动以来的时间来推断设备上次重新启动的日期。然后它使用 NSUserDefaults 在内存中跟踪这个日期。享受!

注意:由于您想在上次启动应用程序后检查这一点因此您需要确保在应用程序启动时调用该方法。最简单的方法是调用下面的方法+(void)initialize {,然后在需要手动检查时调用

#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;
}
于 2018-07-17T23:22:33.067 回答