Is there a clock in iOS that can be used that cannot be changed by the user

后端 未结 6 1095
长发绾君心
长发绾君心 2020-11-30 02:52

I am trying to design a system where real-time events happen and I want to synchronise them to a clock. [NSDate date] would normally be ok but the user could c

6条回答
  •  清歌不尽
    2020-11-30 03:23

    The best monotonically increasing number on an iPhone is mach_absolute_time() which is a count of CPU ticks since the last reboot. If you would like it in seconds for any reason, it is most easily fetched with CACurrentMediaTime().

    Using this to create an "always-increment" is pretty easy. On first launch, store the current value wherever you like. When you exit, and periodically, save the offset from that value. When you restart, check the current value; if it is less than your previous base value, replace your base value (there's been a reboot).

    All of this of course can be cleared by removing your app unless you store something on the server.

    Note that this cannot be used as a strong security measure. There is no way to prevent an authorized user from forging requests. So depending on what you mean by "cheat the system," it may not be a solvable problem. The best you can do is have constant diligence looking for hacks and dealing with them.

提交回复
热议问题