Reputation: 554
Looking to see what people have come up with for this. Basically I want to know if the actual device has been rebooted since my app was last launched. What methods do people use to find this out? (If any?)
I've considered using mach_absolute_time but this is still an unreliable method.
Cheers
Upvotes: 6
Views: 5044
Reputation: 17902
Here is one I made. It takes the current time in GMT and the time since last reboot to extrapolate a date for when the device was last restarted. Then it keeps track of this date in memory using NSUserDefaults. Enjoy!
Note: Since you want to check this since last time app was launched, you need to make sure you call the method anytime the app is launched. The easiest way would be to call the method below in +(void)initialize {
and then also whenever you need to check it manually
#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;
}
Upvotes: 0
Reputation: 1260
Not sure sure if this is what you wanted but have a look at this:
https://github.com/pfeilbr/ios-system-uptime
In this example the author fetches it from the kernel task process.
Or you could look take the mach_absolute_time route, there is an official Apple Q&A with a similar aim https://developer.apple.com/library/mac/#qa/qa1398/_index.html
Hope this helps.
Upvotes: 5