我在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块。
有人能帮我吗?
2条答案
按热度按时间wxclj1h51#
我偶尔会找到解决方案!
try center.startMonitoring(.daily, during: schedule)
的问题是,它只能在选择要阻止的应用程序后才能工作。如果你试图在选择应用程序前开始监控-它不会工作。至少这是我发现的。yks3o0rb2#
您需要告诉DeviceActivityCenter()在哪一天-尝试: