Turns out the default behavior of mach_absolute_time() is to stop ticking once the device goes to sleep. On iOS 6 I was able to extend the time till suspension by 10 minutes when the app was put into the background using beginBackgroundTaskWithExpirationHandler
. In iOS 7, however, the maximum time allowed for this has been capped at 3 minutes (found this out by calling backgroundTimeRemaining
). This is why I was seeing the clock stop on iOS 7, but not on iOS 6.
Stumbled upon this answer today, which seems promising.
- (time_t)uptime {
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
time_t now;
time_t uptime = -1;
(void)time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
uptime = now - boottime.tv_sec;
}
return uptime;
}
time() carries on incrementing while the device is asleep, but of course can be manipulated by the operating system or user. However, the Kernel boottime (a timestamp of when the system last booted) also changes when the system clock is changed, therefore even though both these values are not fixed, the offset between them is.