The listening callbacks were not triggered for intervalDidStart and intervalDidEnd after successfully starting center.startMonitoring when I attempted to set a schedule with DeviceActivitySchedule at 20-minute intervals
Is there anyone who can assist me? Thank you.
Below you will find my code.
let intervalLengthInSeconds = 20 * 60
let intervalEnd = Date(timeIntervalSinceNow: TimeInterval(intervalLengthInSeconds))
let intervalStart = Date()
let schedule = DeviceActivitySchedule(intervalStart: Calendar.current.dateComponents([.hour, .minute], from: intervalStart),
intervalEnd: Calendar.current.dateComponents([.hour, .minute], from: intervalEnd),
repeats: false,
warningTime: DateComponents(minute: 1))
let newActivity = DeviceActivityName(rawValue: "20minuteUse")
Log("😯 \(String(describing: schedule.nextInterval))")
do {
try center.startMonitoring(newActivity, during: schedule)
} catch {
print("failed to start session: \(error.localizedDescription)")
}
class MyMonitorExtension: DeviceActivityMonitor {
let store = ManagedSettingsStore()
// You can use the `store` property to shield apps when an interval starts, ends, or meets a threshold.
override func intervalDidStart(for activity: DeviceActivityName) {
super.intervalDidStart(for: activity)
// Shield selected applications.
Log("😓 start-------")
}
override func intervalDidEnd(for activity: DeviceActivityName) {
super.intervalDidEnd(for: activity)
Log("😓 end-------")
}
override func intervalWillStartWarning(for activity: DeviceActivityName) {
super.intervalWillStartWarning(for: activity)
Log("😓 StartWarning-------")
}
override func intervalWillEndWarning(for activity: DeviceActivityName) {
super.intervalWillEndWarning(for: activity)
Log("😓 EndWarning-------")
}
}
schedule.nextInterval ------ print: 😯 Optional(2024-06-21 08:34:00 +0000 to 2024-06-21 08:54:00 +0000)