The DeviceActivityMonitor did not trigger a callback

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)

Below is all the code; the method "intervalDidStart" has never been called.

Can you help me?

import UIKit
import MobileCoreServices
import ManagedSettings
import DeviceActivity

class MyMonitor: DeviceActivityMonitor {
    let store = ManagedSettingsStore()
    override func intervalDidStart(for activity: DeviceActivityName) {
        super.intervalDidStart(for: activity)
        print("interval did start")
        let model = MyModel.shared
        let applications = model.selectionToDiscourage.applicationTokens
        store.shield.applications = applications.isEmpty ? nil : applications
        store.appStore.maximumRating = 200
    }

    override func intervalDidEnd(for activity: DeviceActivityName) {
        super.intervalDidEnd(for: activity)
        store.shield.applications = nil
        store.clearAllSettings()
    }
}
//
//  MyModel.swift
//  FamilyCotrol
//
//  Created by mac on 2024/6/24.
//

import UIKit

import Foundation
import FamilyControls
import DeviceActivity
import ManagedSettings

class MyModel: ObservableObject {
    static let shared = MyModel()
    let store = ManagedSettingsStore()

    private init() {}

    var selectionToDiscourage = FamilyActivitySelection() {
        willSet {
            print ("got here \(newValue)")
            let applications = newValue.applicationTokens
            let categories = newValue.categoryTokens
            let webCategories = newValue.webDomainTokens
            store.shield.applications = applications.isEmpty ? nil : applications
            store.shield.applicationCategories = ShieldSettings.ActivityCategoryPolicy.specific(categories, except: Set())
            store.shield.webDomains = webCategories
        }
    }

    func initiateMonitoring() {
        let schedule = DeviceActivitySchedule(intervalStart: DateComponents(hour: 16, minute: 0), intervalEnd: DateComponents(hour: 16, minute: 30), repeats: true, warningTime: nil)
        let center = DeviceActivityCenter()
        do {
            try center.startMonitoring(.daily, during: schedule)
        }
        catch {
            print ("Could not start monitoring \(error)")
        }
    }
    
    func stop() {
        store.shield.applications = nil
        store.dateAndTime.requireAutomaticDateAndTime = false
        store.clearAllSettings()
    }
}

extension DeviceActivityName {
    static let daily = Self("daily")
}

You are testing on the managed device (child account), correct?

The DeviceActivityMonitor did not trigger a callback
 
 
Q