How to determine if iphone has been restarted since the last application launch

I would like to detect in my application whether the iPhone has been rebooted since the last launch of my application. I need to do this because my application has been using a timer since the last reboot of the system to synchronize user time, and I want to detect a reboot so that I can invalidate the time.

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

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

+4
source share
4

, :

  • , "tslr" ( , , BTW, ?)
  • , 'ct',
  • ( 'lr'), : lr = ct - tslr
  • store 'lr'

, , 'lr', , , (, , ... a , ).

, , ... , , , "lr" ... , , , 0, . , ...

'lr' :

  • , .
  • , , ( , , ).
+10
    // Returns true if device has rebooted since last time
    private func deviceRebootedSinceLastTime() -> Bool {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let systemUptime = NSProcessInfo.processInfo().systemUptime;
        let timeNow = NSDate().timeIntervalSince1970
        let dateOfLastReboot = NSDate(timeIntervalSince1970: timeNow-systemUptime)

        var didDeviceRebootSinceLastTime = false

        if let storedDateOfLastReboot:NSDate = userDefaults.objectForKey("deviceLastRebootDate") as? NSDate {

            if Int(dateOfLastReboot.timeIntervalSinceDate(storedDateOfLastReboot)) < 1 { //
                print("Reboot time didn't change - date: \(dateOfLastReboot)");
            }
            else {
                print("Reboot time has changed - from: \(storedDateOfLastReboot) to \(dateOfLastReboot)");
                didDeviceRebootSinceLastTime = true

            }
        }
        else {
            print("Reboot time is saved for the first time")
            didDeviceRebootSinceLastTime = true // first time we save value
        }

        userDefaults.setObject(dateOfLastReboot, forKey: "deviceLastRebootDate")
        userDefaults.synchronize() // don't forget this!!!

        return didDeviceRebootSinceLastTime;
    }
+2

- ; , . ( , syslog )

, , ( ), .

+1

Retrieve and save time from either the iPhone or NIST, and to the current runtime from the BSD performance enhancement feature. For NIST time, see. How can I get the real time in the iPhone, and not the time specified by the user in the settings?

If you want to check the reboot, get new values, calculate elapsed time for each and compare elapsed time. Based on the difference, you should be able to determine the reboot.

0
source

All Articles