让iOS系统正常运行,在睡眠时不会暂停 [英] Getting iOS system uptime, that doesn't pause when asleep

查看:201
本文介绍了让iOS系统正常运行,在睡眠时不会暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来在iOS上获得绝对的,始终递增的系统正常运行时间。

I'm looking for a way to get an absolute, always-incrementing system uptime on iOS.

它应该返回自设备上次重启以来的时间,并且不受系统日期更改的影响。

It should return the time since the device was last rebooted, and not be affected by changes to the system date.

我可以找到的所有方法在设备处于休眠状态时暂停(CACurrentMediaTime,[NSProcessInfo systemUptime],mach_absolute_time)或系统日期改变时更改(sysctl / KERN_BOOTTIME)。

All the methods I can find either pause when the device is asleep (CACurrentMediaTime, [NSProcessInfo systemUptime], mach_absolute_time) or are changed when the system date changes (sysctl/KERN_BOOTTIME).

任何想法?

推荐答案

我想我已经解决了。

time()在设备处于睡眠状态时继续递增,但当然可以由操作系统操纵或用户。但是,内核启动时间(系统上次启动的时间戳)也会在系统时钟发生变化时发生变化,因此即使这两个值都不固定,它们之间的偏移也是如此。

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.

#include <sys/sysctl.h>

- (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;

}

这篇关于让iOS系统正常运行,在睡眠时不会暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆