Trigger Action After eventDidReachThreshold

I am trying to configure my app to monitor device screen time and shield apps after a certain amount of screen time is reached.

Here's the scheduled I created:

let schedule = DeviceActivitySchedule(
    // I've set my schedule to start and end at midnight
    intervalStart: DateComponents(hour: 0, minute: 0),
    intervalEnd: DateComponents(hour: 23, minute: 59),
    // I've also set the schedule to repeat
    repeats: true
)

class MySchedule {
    
    var item: NoteItem
    
    init(item: NoteItem) {
        self.item = item
    }
    
    public func setSchedule() {
        print("Setting schedule...")
        print("Hour is: ", Calendar.current.dateComponents([.hour, .minute], from: Date()).hour!)
        
        
        var threshold = DateComponents(hour: item.hour, minute: item.min)

        let events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [
            .discouraged: DeviceActivityEvent(
                applications: NotificationsModel.shared.selectionToDiscourage.applicationTokens,
                threshold: threshold
            )
        ]
        
        

        let center = DeviceActivityCenter()
        do {
            print("Try to start monitoring...")

            try center.startMonitoring(.daily, during: schedule, events: events)
            
            
        } catch {
            print("Error monitoring schedule: ", error)
        }
    }

And here's the shield set up I am currently using:

func setShieldRestrictions() {

        let applications = NotificationsModel.shared.selectionToDiscourage

        store.shield.applications = applications.applicationTokens.isEmpty ? nil : applications.applicationTokens
        store.shield.applicationCategories = applications.categoryTokens.isEmpty
        ? nil
        : ShieldSettings.ActivityCategoryPolicy.specific(applications.categoryTokens)
    }

Currently my shield restrictions are set up so that they are enabled as soon as the user selects which apps they would like to be shielded. Is there a way to reverse that action so that they are only shielded after reaching their threshold for screen time?

The documentation for eventDidReachThreshold is sparse.

https://developer.apple.com/documentation/deviceactivity/deviceactivitymonitor/eventdidreachthreshold(_:activity:)

Yes, you can create a DeviceActivityMonitor app extension and override its eventDidReachThreshold function. The system will invoke your principal class's implementation of that function whenever an event's threshold is reached. You can create one of these extensions in Xcode by clicking File > New > Target > Device Activity Monitor Extension. Xcode will autogenerate a properly configured extension target with a simple implementation of DeviceActivityMonitor that you can then modify to shield apps whenever a threshold is reached.

Trigger Action After eventDidReachThreshold
 
 
Q