nur*_*der 36 swift ios16 xcode14
我刚刚升级到 Xcode 14.0,当我在 iOS 16 设备上运行我们的应用程序时,会调用:
CLLocationManager.locationServicesEnabled()
正在返回警告:
如果在主线程上调用此方法,可能会导致 UI 无响应。相反,请考虑等待回调-locationManagerDidChangeAuthorization:并authorizationStatus首先检查。
如果我必须等待失败/回调而不是直接调用该CLLocationManager.locationServicesEnabled()方法,我需要对代码进行重大更改。这似乎只发生在 iOS 16 设备上。关于如何解决这个问题有什么建议吗?
Yoh*_*hst 37
使用调度队列将其从主队列中移出,如下所示:
DispatchQueue.global().async {
if CLLocationManager.locationServicesEnabled() {
// your code here
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题并使用Async/Await克服了它。
将调用CLLocationManager包装在异步函数中。
func locationServicesEnabled() async -> Bool {
CLLocationManager.locationServicesEnabled()
}
Run Code Online (Sandbox Code Playgroud)
然后相应地更新使用该功能的地方。
Task { [weak self] in
if await self?.locationServicesEnabled() {
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)