NSProcessinfo.h

学习地址:Apple Developer Library - NSProcessinfo

import<Foundation>

1. Description

The NSProcessInfo class also includes the operatingSystem method, which returns an enum constant identifying the operating system on which the process is executing.

NSProcessInfo objects attempt to interpret environment variables and command-line arguments in the user's default C string encoding if they cannot be converted to Unicode as UTF-8 strings. If neither the Unicode nor C string conversion works, these values are ignored by the NSProcessInfo object.

2. Tips

2.1 通过以上我们可知NSProcessInfo可以获得hostname。但是在测试的时候:####

测试设备:iPhone 5s
测试代码:

    CFTimeInterval start = CACurrentMediaTime();
    NSLog(@"%@",[[NSProcessInfo processInfo] hostName]);
    CFTimeInterval end = CACurrentMediaTime();
    NSLog(@"获取hostname用时 - %f 秒",end - start);

测试用时:5.012013 秒

结论:通过NSProcessInfo 收集hostname不太稳定,我觉得还是通过sys/utsname.h - utsname - nodename获取比较好。

2.2 通过NSProcessInfo的systemUptime获取系统启动的时间,但是不调合理####

systemUptime定义:
The amount of time the system has been awake since the last time it was restarted. (read-only)
距离上次系统被唤醒的时间相差多长时间。

但是,这是一篇关于这个问题的帖子:stackoverflow - Getting iOS System Uptime

帖子中给出了一个很好的解决方案:

- (time_t)upTimeSystem
{
    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)) {
        uptime = now - boottime.tv_sec;
    }
    return uptime;
}
posted @ 2016-05-24 16:58  lvable  阅读(252)  评论(0)    收藏  举报