0

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 Answers2

0

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.

0

You need to tell the DeviceActivityCenter() on which day - Try:

DateComponents(hour: startHour, minute: startMinutes, weekday: day)
Boaz Frenkel
  • 618
  • 5
  • 15
  • I did not add the weekDay component. It works fine most of the time but I am not getting the interval end callback sometimes. Will the missing weekDay component be a reason for it ? – Akilan C B Mar 14 '23 at 06:48
  • @AkilanCB Yes, maybe. Try setting the day. What is the error you see when not setting the day? And after setting the day do you still get an error? – Boaz Frenkel Mar 14 '23 at 08:04
  • I'm not getting errors. I am able to register schedule without weekDay and it works fine most of the time. But I have noticed that in some case, the intervalDidEnd is not called as expected on the schedule end. – Akilan C B Mar 14 '23 at 10:55
  • For me, when I set it with the day component this works @AkilanCB And the question said: " 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. " So he is getting an error – Boaz Frenkel Mar 16 '23 at 13:06