0

我在 iOS 中遇到了屏幕时间 API 的问题。我已成功授权使用AuthorizationCenter.shared.requestAuthorization,我已检查使用状态AuthorizationCenter.shared.authorizationStatus,但问题出在DeviceActivityCenter.

在我的 ViewModel 中,我有一个函数,我在检查授权状态后调用它:

func startMonitoringAccordingSchedule() {
        let schedule = DeviceActivitySchedule(intervalStart: DateComponents(hour: 0, minute: 0), intervalEnd: DateComponents(hour: 23, minute: 59), repeats: true)

        let center = DeviceActivityCenter()
        do {
            try center.startMonitoring(.daily, during: schedule)
            print(" Success with Starting Monitor Activity")
        } catch {
            print(" Error with Starting Monitor Activity: \(error.localizedDescription)")
        }
        
    }

问题是,使用成功授权,状态 = 已批准,此功能不起作用,try center.startMonitoring(.daily, during: schedule)失败并进入 catch 块。

有人可以帮我吗?

4

1 回答 1

0

我偶尔找到解决方案!问题try center.startMonitoring(.daily, during: schedule)在于它只能在选择要阻止的应用程序后才能工作。如果您尝试在选择应用程序之前开始监控 - 它将无法正常工作。至少这是我发现的。

于 2022-01-25T10:57:07.160 回答