Is there a way to get system uptime in iOS (using Swift)? What I need is to measure time without having to worry about the user changing the time. In Android there\'s a
Updated for Swift 5 and straight from ADF post here:
func bootTime() -> Date? {
var tv = timeval()
var tvSize = MemoryLayout.size
let err = sysctlbyname("kern.boottime", &tv, &tvSize, nil, 0);
guard err == 0, tvSize == MemoryLayout.size else {
return nil
}
return Date(timeIntervalSince1970: Double(tv.tv_sec) + Double(tv.tv_usec) / 1_000_000.0)
}
"Be aware that this time will change if the system clock changes, that is, the value is the boot time relative to the current system clock."