swift iOS中的设备活动监视器无法正常工作

2nbm6dog  于 2023-02-11  发布在  Swift
关注(0)|答案(2)|浏览(347)

我在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块。
有人能帮我吗?

wxclj1h5

wxclj1h51#

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

yks3o0rb

yks3o0rb2#

您需要告诉DeviceActivityCenter()在哪一天-尝试:

DateComponents(hour: startHour, minute: startMinutes, weekday: day)

相关问题