Device Activity Center doesn't stop monitoring

45 Views Asked by At

i have an application with a timer, the application should lock some apps on the start of the timer and unlock them on the end. Unfortunately while I'm able to shield them, I can't unshield them and I don't understand why. This is the code where I create the shield, as you can see I try to pass a time interval to be able to finish and close the shielding:

import Foundation
import FamilyControls
import DeviceActivity
import ManagedSettings

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

    private init() {}

    var selectionToDiscourage = FamilyActivitySelection() {
        willSet {
            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.webDomainCategories = ShieldSettings.ActivityCategoryPolicy.specific(categories, except: Set())

        }
    }

    func initiateMonitoring(timeInterval: TimeInterval) {
        let end = Date().addingTimeInterval(timeInterval)
        let endComponents = Calendar.current.dateComponents([.hour, .minute], from: end)
        let schedule = DeviceActivitySchedule(intervalStart: DateComponents(hour: 0, minute: 0), intervalEnd: endComponents, repeats: false, warningTime: nil)

        let center = DeviceActivityCenter()
        do {
            try center.startMonitoring(.once, during: schedule)
        }
        catch {
            print ("Could not start monitoring \(error)")
        }
    }
    
    func stopMonitoring() {
        let center = DeviceActivityCenter()
        center.stopMonitoring()
    }
}

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

this is the extension for the device activity monitor:

import MobileCoreServices
import ManagedSettings
import DeviceActivity

class Monitor: DeviceActivityMonitor {
    let store = ManagedSettingsStore()
    override func intervalDidStart(for activity: DeviceActivityName) {
        super.intervalDidStart(for: activity)
        let model = BlockManager.shared
        let applications = model.selectionToDiscourage.applicationTokens
        store.shield.applications = applications.isEmpty ? nil : applications
        print("start monitoring")
    }

    override func intervalDidEnd(for activity: DeviceActivityName) {
        super.intervalDidEnd(for: activity)
        store.shield.applications = nil
        store.shield.applicationCategories = nil
        store.shield.webDomainCategories = nil
        store.shield.webDomains = nil
    }
    
    override func eventDidReachThreshold(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) {
        super.eventDidReachThreshold(event, activity: activity)
        store.shield.applications = nil
        store.shield.applicationCategories = nil
        store.shield.webDomainCategories = nil
        store.shield.webDomains = nil
    }
}

someone knows why the shielded app remain stuck shielded?

0

There are 0 best solutions below