skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. You need to tell the DeviceActivityCenter() on which day –
    Try:

    DateComponents(hour: startHour, minute: startMinutes, weekday: day)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search