I'd like a way of creating a local native-iOS time-hack proof background count-down timer, for example for counting down times for next reward in games.
Explanation of the object:
local
- Works without internet connection
native-iOS
- I want to find a solution in (preferably) Objective-C or Swift
time-hack proof
- When a user changes its device time forward / backward, remaining time stays the same
background
- Shut-down / re-open friendly
count-down timer
- A timer which, after start, will provide a method for checking the remaining time
I'd like to have a time-based reward system in my iOS game, which won't be easily hackable just by moving the device time forward.
Originally, I thought this was not possible and kept using [NSDate date]
and savedDate
, comparing them together to find elapsed time. Recently, however, I came across a game which uses this feature: Crossy Road. That is a Unity game, as far as I know, but I doubt that a somewhat common feature like this available in Unity project which is compiled into an iOS app is not accessible on iOS via Objective-C or Swift.
They provide a timed reward system, once per every 6 hours, and, as far as I tested, it's not hackable by changing the device time. It also works without an internet connection. (I'm not sure if it works if you don't ever connect, but when I connected, disconnected, and tried to use the hack, it didn't work.)
You could use the current system uptime (mach_absolute_time()
). This function is easily accessible using CACurrentMediaTime()
which returns it handily in seconds. Then you probably have to set up some kind of interval to check this value. This can of course be done using NSDate since it's not a problem that it gets triggered by the user setting the clock forward. When it comes to restarts, simply save the start value and use that as an offset after reboot.
The only drawback is that it doesn't count the time the device is turned off but that's generally not a big deal these days when no one turns of their phones.
To get the actual timer function, simply save the value at the start and then check it regularly against the projected end value, perhaps with increasing resolutions as the end approaches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With