iOS时间更改通知和以前的时间 [英] iOS Time change notification and previous time

查看:147
本文介绍了iOS时间更改通知和以前的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以与超时一起使用的应用程序(例如->在3天内振铃 )。如果用户在离线更改设备本地时间,会发生我的问题,因为我正在比较两个NSDate (最终日期和今天)进行检查那超时。

I have an application that works with timeout (let's say -> ring in 3 days). My problem occurs if a user change the device local time while being offline because I am comparing two NSDate (the final date and today) to check that timeout.

因此,我试图何时用户进行重要的时间更改(几个小时或几天)以得到通知为了根据更新之前 之后 之间的差异更新我的计时器。

So I am trying to get notified when a user made a significant time change (several hours or days) in order to update my timers depending on the difference between before and after the update.

我找到了 UIApplicationSignificantTimeChangeNotification ,它可以:


发布时间有重大变化时,例如,更改为新的一天(午夜),更新运营商时间,以及更改为夏令时或从夏令时更改。

Posted when there is a significant change in time, for example, change to a new day (midnight), carrier time update, and change to or from daylight savings time.

适合,但我不知道如何获得旧时光以获取差异。我也不能依赖网络服务器,因为该应用程序应该以脱机模式运行。

That nearly fits but I can't figure out how to get the "old" time in order to get the difference. I can't rely on a webserver neither because the app should be running in offline mode.

任何想法或建议都欢迎;)

Any idea or suggest is welcome ;)

推荐答案

我终于找到了一种方法来解决问题,方法是使用 CPU时间(除非重新启动,否则它总是不断增加)。

I finally find a way to solve my problem by using the CPU time (which is always constantly increasing except on reboot).

首次启动应用程序时,我获得了实时时间和CPU时间。

First time the app is launched, I get the Real time and the CPU time.

然后,当我需要检查时间是否已更改时,我只是选择

Then, when I need to check if time has been changed, I just take


首先是

实时,(当前CPU时间-最初是CPU时间)。

the Real time at first, I add to it ( the current CPU time - the CPU time at first).

这给了我正确的时间。

This give me the correct time.


注意:请注意使用实际的CPU时间,而不要使用计时器所用的时间,因为它会在设备进入睡眠状态时停止。 p>

Note: take care to use the real CPU time, not the one used by timer because it's stop when device go to sleep.

这篇关于iOS时间更改通知和以前的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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