ios - 如何获取 iOS 重启时间?

标签 ios unix reboot

重启次数是指当用户重启设备时,重启次数会累计一次。

那么,有没有办法可以获取 iOS 设备的重启时间?

最佳答案

使用此方法,您可以获得自系统上次重启以来的时间。

+ (time_t)getTimeSinceLastBoot {
    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;
}

要获得确切的日期,您可以使用上述方法:

    long totalSeconds = [self getTimeSinceLastBoot];
    NSDate *dateNow = [NSDate date];
    NSDate *date = [NSDate dateWithTimeInterval:-totalSeconds sinceDate:dateNow];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateString = [formatter stringFromDate:date];

通过上述方法,您可以定期将时间保存到持久存储中的某个位置,并每次检查,如果您下次得到相同的时间,则表示设备没有重新启动。如果您得到不同的时间,则只需将该时间添加到您的数据库中即可。

不要忘记包含以下文件:

#include <sys/param.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/socket.h>

关于ios - 如何获取 iOS 重启时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33864550/

相关文章:

ios - UIPickerView 没有出现在最上面

windows - 如何检测 Windows 服务器在重启后是否可用?

php - 如何从 php 文件重新启动 linux

android - 手机重启后设置闹钟

iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法

ios - 导航栏为白色,同时 isTranslucent = false

macos - 在 Mac OS X Lion : "error: CPU you selected does not support x86-64 instruction set" 上安装 libmad

linux - 如果 `so`代表共享对象,那么 `a`代表什么?

ios - 你如何找出一个对象的类型

Python 在 sudo 中调用 bash 脚本的子进程