views:

320

answers:

3

Hi,

I'd like to detect from within my app whether the iPhone has been rebooted since last time my app was started. I need to do this because my app uses the timer since last system reboot to clock a user's time and I want to detect reboot so I can invalidate the time.

Is there anywhere I could extract the information from the system console log like reboot , crashes ? The organizer in xcode can access it , maybe I can too.

If not , can you think of other ways to get this information?

A: 

Get and save the time either from the iPhone or from NIST and the current runtime from the BSD uptime function. For NIST time see http://stackoverflow.com/questions/1443350

When you want to check for a reboot get new values of these, compute the elapsed time for each and compare the elapsed times. Based on the difference you should be able to determine a reboot.

zaph
In my case that wouldn't work, because I can't depend on internet connection and can't depend on the iPhone time , because that can be tampered with by the user.
Maxm007
The user can also tamper with your application. How secure do you need this to be?
Amuck
No the user can't tamper easily with my app, because it is digitally signed. It definitly needs to be more secure than changing the iPhone time in Settings -> General -> Time
Maxm007
+5  A: 

This seems like it would work:

  • get the time since last reboot, and for this example, let's store it in a variable called 'tslr' (duration in milliseconds I guess, BTW, how do you get that?)
  • get the current time, store it in variable 'ct' for example
  • compute the last reboot time (let's call it 'lr'), we have: lr = ct - tslr
  • store 'lr'

Next time your application gets started, load the previous value for 'lr', compute the new one, and if they differ, you have detected a reboot (you'll probably have to tolerate a small difference there... a couple milliseconds perhaps).

I think it would be pretty tough to fool that... the user would have to tamper their phone time very precisely, and they would have to start your application at a very precise moment on top of that, exactly when the new 'lr' would be identical to the previous one... pretty tough to do, the probability of them being able to do that is very close to 0 I think. And you don't need any internet connection to do that...

The new 'lr' would be identical to the previous one in the following cases only:

  • phone was not rebooted, and time was not changed
  • time was tampered with, AND the user managed to start your application at the precise millisecond to fool your algorithm (chances of that happening more than ultraslim)
Zoran Simic
Brilliant. Works perfectly and at the same time detects user changing iphone date. Now I can invalidate any time a user scored that has been tampered with without network connection.Btw: you can get the time since reboot using a mach timer :http://discussions.apple.com/thread.jspa?threadID=1632831
Maxm007
Hmm, this mach timer is strange. Even though my iphone was on all night and didn't reboot, the timer seems to have reset.
Maxm007
+1  A: 

Zoran's answer is the right way to go; it's the closest you are going to get without a network connection. (neither the cellular subsystem, nor the syslog are accessible for security reasons)

If you are looking to prevent malicious users from generating fake time data, have some central server (or trusted local server for enterprise deployments) track time-related events for you.

rpetrich