Posts

Post not yet marked as solved
1 Replies
1.1k Views
I'm looking for a way to check user Screen Time and trigger a notification after a specified amount of time on certain apps. I have the code from WWDC21 but I was wondering if I could get the code sample for the Worklog app created for WWDC22. Would really help as a reference point for building my project. Thank you!
Posted
by blanny21.
Last updated
.
Post not yet marked as solved
1 Replies
663 Views
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:)
Posted
by blanny21.
Last updated
.