Apple Screentime API - iOS

1.4k Views Asked by At

Does anyone have a sample working code for the screen time api? My app uses Family controls framework to get parental control authorisation. Once authorised, I create a schedule with events and start monitoring it.

var scheduleDuration = 60 //assumption
for usageThreshold in stride(from: 1, to: scheduleDuration, by: 1) {
    let usageEvent1 = DeviceActivityEvent.init(threshold: .init(minute: usageThreshold))
    usageEvents[DeviceActivityEvent.Name.init(rawValue:"scheduleID~\(usageThreshold)")] = usageEvent1
}


let schedule = DeviceActivitySchedule(intervalStart: intervalStart, intervalEnd: intervalEnd, repeats: false)
let activityName = DeviceActivityName.init(rawValue: "\(scheduleID)")
    
do {
    try deviceActivityCenter.startMonitoring(activityName, during: schedule,events: usageEvents)
}catch let error {
    NSLog("Error monitoring device schedule - \(error.localizedDescription)")
}

I have added DeviceActivityMonitor extension. It works sometimes but suddenly stop receiving callbacks in the activity monitor extension. Any help is appreciated :-)

0

There are 0 best solutions below