如何获得iPhone中的实时时间,而不是用户在设置中设置的时间? [英] How can I get the real time in iPhone, not the time set by user in Settings?

查看:287
本文介绍了如何获得iPhone中的实时时间,而不是用户在设置中设置的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确保我的应用获取正确的时间,而不是用户在设置中设置的时间。



例如,一旦用户启动应用,我想得到一个时间戳。用户将看到每秒更新的定时器屏幕上的经过时间。当用户退出应用程序,并在一段时间后重新启动应用程序,计时器将只获取当前时间,并与它保持的原始时间戳进行比较。所以基本上,就像iPhone上的Clock.App中的秒表。



现在,clock.app的问题是,如果用户在设置一小时,这将影响定时器。显然我不想要,因为在我的应用程序,这将是欺骗。



我需要得到一个更可信的时间源比较。 p>

我可以从互联网上得到时间。但问题是,我需要互联网连接,所以只有工作,如果有一个互联网连接。我的应用需要优先在离线工作。



有没有某种内部时钟可以访问?



是否有检查用户是否在设置中更改日期时间的方法?





感谢

解决方案

正在使用计时器自上次iphone启动以来的时间单位的计时器。



这很好。唯一的限制是,用户不能被允许重新启动或它会使他的时间失效。



我检测到重新启动,最初存储与马赫计时器相关联的iphone时间戳,然后检查每次应用程序启动时,所以它没有更改。这有一个副作用,如果用户更改iPhone DateTime,而他的时间,这也将使分数失效,但这是确定。



如何检测iPhone是否自上次应用程式启动以来已重新启动



我可以轻易警告使用者:重新启动或更改iphone时间,您的得分时间。


I need to ensure that my app gets the correct time, not the time set by the user in settings.

So for instance, once the user starts the app, I'd want to get a timestamp. The user would see the elapsed time on a timer on screen updated every second. When the user exits the app, and after some time restarts the app , the timer would just get the current time and compare it with the original timestamp it kept. So basically, just like the stopwatch in the Clock.App on iPhone.

Now the problem with the clock.app is that if the user goes and advances the time in Settings by one hour, this will influence the timer. Obviously I don't want that, because in my app, that would be cheating.

I'd need to get a more trustworthy time source to compare to.

I could get the time from the internet. But the problem is that I'd need internet connection , so only works on if there is an internet connection. My app needs to work offline preferably.

Is there some kind of internal clock I can access?

Is there a way of checking whether the user has changed the date time in Settings?

Any other ideas?

Thanks

解决方案

The best solution I've come up with is using the mach timer which counts time units since last iphone boot.

This works great. The only restriction is that the user cannot be allowed to reboot or it would invalidate his time.

I detect a reboot by initially storing the iphone timestamp associated with the mach timer and then checking every time the app starts, so it hasn't changed. This has as a side effect that if the user changes iPhone DateTime while he's being timed , that will also invalidate the score, but that is ok.

How can I detect whether the iphone has been rebooted since last time app started

I can easily warn my users about this: rebooting or changing iphone time while you're on the clock will invalidate your scored time.

这篇关于如何获得iPhone中的实时时间,而不是用户在设置中设置的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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