0

I need to identify if the device is rebooted. Currently saving time in the database, and periodically check time interval from last boot using the following code as suggested in Apple forums:

func bootTime() -> Date? {
    var tv = timeval()
    var tvSize = MemoryLayout<timeval>.size
    let err = sysctlbyname("kern.boottime", &tv, &tvSize, nil, 0);
    guard err == 0, tvSize == MemoryLayout<timeval>.size else {
        return nil
    }
    return Date(timeIntervalSince1970: Double(tv.tv_sec) + Double(tv.tv_usec) / 1_000_000.0)
}

But the problem with this is even without a reboot, tv.tv_sec value differs around 30 (it varies from 0 seconds to 30 secs).

Anybody have any idea about this variation? or any other better way to identify device reboot without using sysctl or other reliable sysctl.

https://developer.apple.com/forums/thread/101874?answerId=309633022#309633022

Any pointer is highly appreciated.

I searched over SO, all the answer points to the solution mentioned here. Which have the issue as I mentioned. Please don't mark duplicate.

Manish Punia
  • 747
  • 4
  • 9
  • Any reason why you would like to know the time since the last reboot? – user1046037 Jan 07 '21 at 07:02
  • There is some operation is needed to perform after device reboot (secret.. :P) . Actually, I need to detect if the device is rebooted after a particular time (which is saved in the database). This is the only solution been suggested on different forums. – Manish Punia Jan 07 '21 at 07:07
  • 1
    Possible duplicate: https://stackoverflow.com/questions/1443601/how-can-i-detect-whether-the-iphone-has-been-rebooted-since-last-time-app-starte – Adnan Ahmed Jan 07 '21 at 07:13
  • Please at least read the description of the question, I mentioned clearly about the possible duplicate. – Manish Punia Jan 07 '21 at 07:41

0 Answers0