i am using monitor to calculate screen time, so it can be used in other target, because the screen time report extension can't share data outside the sand box.
but i have found that it's no accurate for some user, it will calculate more time then the screen time report extension.
this is the code of how i did it:
center.stopMonitoring([ DeviceActivityName("activity.daily.screentime")])
// Set up the events list. All event names should be unique, but they can be shared between schedules.
var events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [:]
for quickMinute in [1, 3] {
let eventName = DeviceActivityEvent.Name("event.screentime.thresholdReach.\(quickMinute)")
events[eventName] = DeviceActivityEvent(
threshold: DateComponents(minute: quickMinute),
includesPastActivity: true
)
}
for minute in stride(from: 5, through: 1135, by: 5) {
let eventName = DeviceActivityEvent.Name("event.screentime.thresholdReach.\(minute)")
events[eventName] = DeviceActivityEvent(
threshold: DateComponents(minute: minute),
includesPastActivity: true
)
}
let schedule = DeviceActivitySchedule(
intervalStart: DateComponents(hour: 0, minute: 0),
intervalEnd: DateComponents(hour: 23, minute: 59),
repeats: true
)
let activity = DeviceActivityName("activity.daily.screentime")
try center.startMonitoring(activity, during: schedule, events: events)
then in the DeviceActivityMonitorExtension, i will extract the minute from "event.screentime.thresholdReach.(minute)" in eventDidReachThreshold.
this code works on my own device, but not works on my friend's device, the time is just much more then the report.
Topic:
App & System Services
SubTopic:
General
Tags: