- Home /
Countdown for iPhone that continues while the apps is closed
I'm currently working on a game on iPhone with resources regained over time like a few game already do. I was wondering how do you make a countdown that continue while the apps is closed. I've read about the System.DateTime.Now that tell you the current real time on the machine but it never seemed to work like I wanted. If someone could give me hints or like me to a similar thread, it would be really appreciated because all I've found for the moment is how to make a basic timer...
Thanks for your help!
Answer by flamy · Feb 02, 2012 at 02:33 PM
System.DateTime.Now.ToUniversalTime(); this function returns the current time in a universal format. what you have to do is save it to local file (or playerprefs) and reload it back when you open the app again get the time using the same function. Now is you find the difference between these you will get the time offline, subtract it from the countdown time you have saved early to the local. The difference will be in System.TimeSpan so should know abt tht too
example:
this code i used to retrieve the difference in minutes
string temp=PlayerPrefs.GetString("lastoffline");
double lastMinutes = System.Double.Parse(temp);
System.DateTime dt2 = System.DateTime.Now.ToUniversalTime();
System.TimeSpan dt3 = dt2-((new System.DateTime(0))).ToUniversalTime();
double currentMinutes = dt3.TotalMinutes;
double totalOffline=currentMinutes-lastMinutes;
// this code in start..
previously i have saved it from the OnApplicationQuit method of monobehaviour
System.DateTime dt = System.DateTime.Now.ToUniversalTime();
System.DateTime dt1 = new System.DateTime(0).ToUniversalTime();
System.TimeSpan time1 = dt-dt1;
double lastMinutes = time1.TotalMinutes;
PlayerPrefs.SetString("lastoffline",(lastMinutes).ToString());
im changing double to string because playerprefs doesnt have option to save double,hence saving it as string and retrieving original data on loading
I tried to add this in my app but im coding in Javascript so I get some errors... I changed a few things, following the errors message, but I still can't figure out how to convert or use the doubles and the Double.Parse method...
Try these resources for converting: http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/
Thanks alot for the link, I figured out late yesterday that I had to use System.Double ins$$anonymous$$d of double, now everything works fine!
Answer by cupsster · Feb 02, 2012 at 02:02 PM
Try to store time from internet source when application closes and on application load get time again. After this make your desired computation for gained resources. You can extend this idea to hash/crypt time in player preferences along with comparision of time on device to be sure user does not cheat. If watching time is crucial for your app you must be carefull about not let app fooled by user. i think that online time check is cruial for this.
Your answer
Follow this Question
Related Questions
Calling methods 0 Answers
Timer doesn't work properly 2 Answers
When Score goes up by 25 change timer value 1 Answer
Countdown Timer Help About putting 0's 1 Answer
Timer counts down unwanted 2 Answers