Getting system uptime in iOS/Swift
As you ask for a pure-Swift solution, I converted the ObjC code from the answer you mentioned Getting iOS system uptime, that doesn't pause when asleep.
func uptime() -> time_t {
var boottime = timeval()
var mib: [Int32] = [CTL_KERN, KERN_BOOTTIME]
var size = strideof(timeval)
var now = time_t()
var uptime: time_t = -1
time(&now)
if (sysctl(&mib, 2, &boottime, &size, nil, 0) != -1 && boottime.tv_sec != 0) {
uptime = now - boottime.tv_sec
}
return uptime
}
// print(uptime())
To make it a bit prettier, we can use sysctlbyname
instead of sysctl
:
// var mib: [Int32] = [CTL_KERN, KERN_BOOTTIME]
sysctlbyname("kern.boottime", &boottime, &size, nil, 0)
Getting iOS system uptime, that doesn't pause when asleep
I think I worked it out.
time()
carries on incrementing while the device is asleep, but of course can be manipulated by the operating system or user. However, the Kernel boottime (a timestamp of when the system last booted) also changes when the system clock is changed, therefore even though both these values are not fixed, the offset between them is.
#include <sys/sysctl.h>
- (time_t)uptime
{
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
time_t now;
time_t uptime = -1;
(void)time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
uptime = now - boottime.tv_sec;
}
return uptime;
}
How to get exact time since iOS device booted?
After investigation of the data you provide. It is possible to conclude that CMLogItem
:
The CMLogItem class is a base class for Core Motion classes that
handle specific types of motion events. Objects of this class
represent a piece of time-tagged data that can be logged to a file.
CMLogItem defines only a read-only timestamp property that records the time a motion-event measurement was taken. Not the exact time since device was booted.
By ProcessInfo.processInfo.systemUptime
you going to get time from the last time it was restarted.
I believe data that you would like to obtain is quite specific and is under Private API of the iOS.
In iOS, using Swift, how do you get the time of iPhone boot?
From the comments, I was able to find ProcessInfo.processInfo.systemUptime
which is the time since last boot. From this, and Date()
, I can get the time of start via:
time_of_last_boot = Date() - ProcessInfo.processInfo.systemUptime
NSProcessInfo().systemUptime is wrong
I use this method and it helps me
public static func uptime() -> (days: Int, hrs: Int, mins: Int, secs: Int) {
var currentTime = time_t()
var bootTime = timeval()
var mib = [CTL_KERN, KERN_BOOTTIME]
// NOTE: Use strideof(), NOT sizeof() to account for data structure
// alignment (padding)
// http://stackoverflow.com/a/27640066
// https://devforums.apple.com/message/1086617#1086617
var size = strideof(timeval)
let result = sysctl(&mib, u_int(mib.count), &bootTime, &size, nil, 0)
if result != 0 {
#if DEBUG
print("ERROR - \(__FILE__):\(__FUNCTION__) - errno = "
+ "\(result)")
#endif
return (0, 0, 0, 0)
}
How to get NSTimeInterval value from last boot
The NSTimeInterval value since the last system restart can be acquired more directly via the following Foundation object and method:
[[NSProcessInfo processInfo] systemUptime]
Related Topics
Undefined Symbols for Architecture Arm64 "_Objc_Class_$_Sfsafariviewcontroller"
Deprecated Warnings in Xcode and How to Handle Deprecation
How to Sign Out of Apple After Being Authenticated
How to Use Avcapturephotooutput to Capture Photo Swift + Xcode
Position a Subview on the Edge of a Circular Shaped View
How to Save Custom Objects in Array and Store It in Nsuserdefaults - Iphone
How to Have Firebase Automatically Delete Values Older Than 30 Minutes
Duet - Merge 2 Videos Side by Side
Get Google Contacts Using API on iOS
Detecting iOS Version Number from User Agent Using Regular Expressions