IOS: Calculate time interval without using system time

Hi, I’m developing game in which I give free gifts to players every 3 hours.
For this I’m using System.DateTime and System.TimeSpan classes. All is working good except one thing: player can change data time in his iPhone/iPad and get gifts every minute, because algorithm thinks 3 hours interval was passed.
I check this situation in few Unity games (for example, Crossy Road) and some how they managed this problem: system time doesn’t affect calculation of free gift interval.

Maybe someone know how? The only idea is they are using Push Notifications, perhaps this is the key to calculate time?

Thanks.

I’d imagine Crossy Road is getting the time from the internet. Getting a WWW response from time.is, or something.

1 Like

Thanks

You must use a distant server to do this. Your server must store the player last gift time (let’s say last_time_gift).

Then :

  • Send a request to the server
  • The server check if it’s time to give a gift : server_time() - last_time_gift > 3 hours
  • Update the last_time_gift on the server and send back the response (success or error)

Thanks. The idea with server is clear.
But, I check how Crossy Road and Sling Kong behave without internet connection (in Airplain Mode) and discover next: they still able to calculate time intervals correctly.

When I read an article about Android Push Notifications, it was mentioned, that push notifications is like a separate thread and can work all time even when App is closed. Maybe iOS Push Notifications works same way? Maybe it’s possible to run a daemon process wich could calculate time?

I found this article about iOS Push Notifications very interesting: IOS Push Notification Tutorial. - Unity Engine - Unity Discussions

Also is’t good to save last date when player collects gift, and if current date is less than saved - most probably this is a cheat: player sets date in future, come to game, collect gifts, closes game, returns to real date. Of course this is no a 100% guarantee, but it forces player to do more iterations to achieve “cheat gifts”.

Simply fake it of the player can’t reach the internet. Count down for a single gift, then don’t reset until you can contact your www.

As BoredMormon said, you have to fake if the user has not internet.

  • Every 3 hours, locally on the device (timer based on current phone time), trigger a function
  • In this function :
  • First, try to reach your server
  • If internet is enabled, you can calculate the gift like I said in my previous post
  • Second, if internet IS NOT reachable OR (internet is reachable AND gift has been gave)
  • Emit a local notification

If the player change his local time, a notification will be send, but when internet will be available the server will say ‘hey man, it’s not true !’. So you’re good :wink:

Thanks for answers :slight_smile: