本文介绍了在iOS / iPhone上冻结正常运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么我使用以下方法遇到奇怪的正常运行时间吗?

Would someone know why I'm experiencing weird uptime with the following method?

NSProcessInfo *processInfo = [NSProcessInfo processInfo];
NSTimeInterval systemUptime = [processInfo systemUptime];

第一分钟,一切似乎都很好,但是当我回到应用程序的时间或日子时,正常运行时间仍然是相同的:30分钟,或1小时34分...它似乎随机冻结。主要在iPhone 4上(很少在模拟器或iPad上)

For first minutes, everything seems fine, but when I come back on the app hours or days laters the uptime is still the same : 30min, or 1h34... it seems to freeze at a random moment. Mostly on iPhone 4 (rarely on Simulator or iPad)

它可能与我的显示方式有关:

It could be linked to my way of showing it:

+ (NSTimeInterval)uptime:(NSNumber **)days hours:(NSNumber **)hours mins:(NSNumber **)mins
{
    NSProcessInfo *processInfo = [NSProcessInfo processInfo];
        //START UPTIME///////
    NSTimeInterval systemUptime = [processInfo systemUptime];
        // Get the system calendar
    NSCalendar *sysCalendar = [NSCalendar currentCalendar];
        // Create the NSDates
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:(0-systemUptime)];
    unsigned int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *c = [sysCalendar components:unitFlags fromDate:date toDate:[NSDate date]  options:0];
        //NSString *uptimeString = [NSString stringWithFormat:@"%dd %dh %dmin", [c day],[c hour],[c minute]];
    *days = [NSNumber numberWithInt:[c day]];
    *hours = [NSNumber numberWithInt:[c hour]];
    *mins = [NSNumber numberWithInt:[c minute]];
    [date release];
        //END UPTIME////////

    return systemUptime;
}

以后的代码:

NSNumber *uptimeDays, *uptimeHours, *uptimeMins;
[CJGDevice uptime:&uptimeDays hours:&uptimeHours mins:&uptimeMins];
NSString *uptimeString = [NSString stringWithFormat:@"%@d %@h %@min",
                          [uptimeDays stringValue],
                          [uptimeHours stringValue],
                          [uptimeMins stringValue]];

编辑:在iPad和iPhone上录制结果3天后我可以看到正常运行时间错误,时间运行得太慢,我们等待得越多,显然已经晚了

after 3 days recording the results on iPad and iPhone I can see that this uptime is wrong, the time is running too slowly, the more we wait the more it's obvious that it's late

推荐答案

这种方法可以解决问题: / p>

This method does the trick:

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

感谢Alastair Stuart的。

Thanks to Alastair Stuart for the link.

这篇关于在iOS / iPhone上冻结正常运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:58