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