获取目标c中的启动时间

You*_*sef 3 boot objective-c ios

如何在目标c中获得ios的启动时间?

有办法搞定吗?

Jer*_*myP 11

不知道这是否适用于iOS,但在OS X(本质上是相同的操作系统)中,您将使用sysctl().这就是OS X Unix实用程序的uptime功能. 源代码可用 - 搜索"boottime".

#include <sys/types.h>
#include <sys/sysctl.h>  

// ....  

#define MIB_SIZE 2  

int mib[MIB_SIZE];
size_t size;
struct timeval  boottime;

mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
if (sysctl(mib, MIB_SIZE, &boottime, &size, NULL, 0) != -1)
{
    // successful call
    NSDate* bootDate = [NSDate dateWithTimeIntervalSince1970:boottime.tv_sec];
}
Run Code Online (Sandbox Code Playgroud)

iOS沙盒环境中编程的限制性可能会使它无法正常工作,我不知道,我还没有尝试过.

  • 似乎工作,我编辑了答案,以显示如何将其作为NSDate (2认同)
  • 它绝对允许在应用商店中使用 (2认同)