我需要许可才能使用iOS的提醒,为此,我正在这样做:
switch EKEventStore.authorizationStatus(for: .reminder) {
case .authorized:
print("Access granted")
//everything's normal here
//executing my function here
case .denied:
print("Access denied")
case .notDetermined:
print("not defined yet")
//No determined so asking for permission
self.eventStore.requestAccess(to: .reminder) { (granted, error) -> Void in
if granted == true {
print("permission granted")
//executing my function here after getting permissions but this piece of code executes after a long delay
//this piece of codes are executing after a while say 5-10 seconds
}else if error != nil{
print("ther's an error : \(error)")
}
}
default:
print("Case Default")
} 如前所述,当应用程序提示用户允许提醒并授予用户权限时,我的下一个函数被执行了,但过了一段时间(5-10秒)
有人能解释为什么会发生这种事吗?
发布于 2016-12-15 15:33:37
requestAccess的完成不会在主线程上被调用。将permissions granted代码放入调度异步:
DispatchQueue.main.async {
print("permission granted")
}发布于 2016-12-15 15:33:24
请求权限纯粹是一个同步的过程,您不能立即执行该函数,因为这不能从代码中控制。应用程序代码可以请求权限,当操作系统授予权限时,我们会得到委托回调处理程序,基于此,实际接收请求的权限会出现滞后。
还可能是从线程/块请求权限,该线程/块没有在主UI线程上运行,并且在执行该代码时会出现不可见的延迟。必须检查启动权限请求的代码。
https://stackoverflow.com/questions/41167357
复制相似问题