iOS:如何衡量通过时间,与时钟和时区变化无关? [英] iOS: How to measure passed time, independent of clock and time zone changes?

查看:123
本文介绍了iOS:如何衡量通过时间,与时钟和时区变化无关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了测量我的应用程序运行时的持续时间,以及我的应用程序在后台闲置时所经过的时间,我需要一个参考时钟,用户不会更改时间+日期他的日历时钟。

In order to measure the time duration while my app is running, as well as the time that passed while my app was idle in the background, I need a reference clock that is not altered by the user changing the Time+Date of his calendar clock.

我不能依赖NSDate,因为当我的应用程序在后台时,用户可以更改它(不,拦截与此类相关的通知)时钟变化对我的需求来说太过分了。)

I can not rely on NSDate because that can be changed by the user while my app is in the background (and no, intercepting the notifications related to such clock changes is overkill for my needs).

我需要的是一个功能,例如 启动后的秒数 操作系统。 Mac OS提供了这样的功能( Ticks Microseconds ),但iOS呢?

What I need is a function such as "seconds since boot of the OS". Mac OS offer such functions (both Ticks and Microseconds), but what about iOS?

推荐答案

#include< mach / mach_time.h> 到你的源文件中。

replace任何对 clock_gettime(CLOCK_MONOTONIC,& timespec_t)的调用,调用 mach_absolute_time()

replace any calls to clock_gettime(CLOCK_MONOTONIC, &timespec_t) with calls to mach_absolute_time().

这篇关于iOS:如何衡量通过时间,与时钟和时区变化无关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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