Hi, has anyone tried getting system uptime on iOS ...
# multiplatform
p
Hi, has anyone tried getting system uptime on iOS using KMM? I'm using SystemClock.elapsedRealtime() for Android. https://developer.apple.com/documentation/foundation/nsprocessinfo/1414553-systemuptime?language=objc - this API only returns the active time. I found some objective C and Swift snippets for getting the total uptime but I can't get them to work in Kotlin https://developer.apple.com/forums/thread/101874
In the end, I used Koin and an interface to use a native iOS code: class SystemInfoImpl: SystemInfo { func getDeviceUptime() -> Int64 { var uptime = timespec() if clock_gettime(CLOCK_MONOTONIC_RAW, &uptime) == 0 { return Int64(uptime.tv_sec) * 1000 + Int64(uptime.tv_nsec / 1000000) } return -1 } }
1
p
You can always bridge to the platform native API, that's the strength of KMP but wonder if
org.jetbrains.kotlinx:kotlinx-datetime
works for you?
p
Hi Pablichenko. I've checked the docs for
datetime
but haven't seen any references to system uptime or boot timestamp.
p
Ah I see, I initially thought you wanted to measure just time elapsed. But not specifically system uptime. Actually I thought this was possible too but doesn't seem to be the case 😕
🫡 1