I have faced a problem with Screen Time API in iOS. I have successfully authorised using AuthorizationCenter.shared.requestAuthorization
, I have checked status using AuthorizationCenter.shared.authorizationStatus
, but the problem is with DeviceActivityCenter
.
In my ViewModel I have a function, that I’m calling after checking Authorisation Status:
func startMonitoringAccordingSchedule() {
let schedule = DeviceActivitySchedule(intervalStart: DateComponents(hour: 0, minute: 0), intervalEnd: DateComponents(hour: 23, minute: 59), repeats: true)
let center = DeviceActivityCenter()
do {
try center.startMonitoring(.daily, during: schedule)
print("ðŸ˜ðŸ˜ðŸ˜ Success with Starting Monitor Activity")
} catch {
print("ðŸ˜ðŸ˜ðŸ˜ Error with Starting Monitor Activity: (error.localizedDescription)")
}
}
The problem is that with Succeed Authorisation, with Status = Approved, this function doesn’t work, try center.startMonitoring(.daily, during: schedule)
fails and it goes to the catch block.
Can anybody help me with that?
2
Answers
I found solution occasionally! The problem with
try center.startMonitoring(.daily, during: schedule)
is that it can work only after selecting apps to block. If you try to start monitoring before you select apps - it won't work. At least this is what I found.You need to tell the DeviceActivityCenter() on which day –
Try: