且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得iPhone中的实时时间,而不是用户在设置中设置的时间?

更新时间:2022-10-18 20:03:13

正在使用计时器自上次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.