2

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 :-)

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
Akilan C B
  • 199
  • 1
  • 9
  • 1
    There are [Meet the Screen Time API](https://developer.apple.com/videos/play/wwdc2021/10123/) and [What's new in Screen Time API](https://developer.apple.com/videos/play/wwdc2021/10123/) from Apple. – Ranoiaetep Nov 25 '22 at 07:51
  • @Ranoiaetep, I did look into those. I just followed what they have said. It works at first but suddenly callbacks to my device activity extension gets stopped for some reason. – Akilan C B Nov 28 '22 at 05:48

0 Answers0