[英]CLLocationManager requestLocation button is throwing errors on iOS 15 Xcode 13 Beta 2
I am trying to request one time access to the location.我正在尝试请求一次访问该位置。 I have made the necessary additions to the info.plist file as shown below:
我已经对 info.plist 文件进行了必要的添加,如下所示:
Now, when I use the following code to call updateLocation I get the following error:现在,当我使用以下代码调用 updateLocation 时,出现以下错误:
[MKCoreLocationProvider] CLLocationManager(<CLLocationManager: 0x600001cf4470>) for <MKCoreLocationProvider: 0x600002ce03f0> did fail with error: Error Domain=kCLErrorDomain Code=1 "(null)"
The operation couldn’t be completed. (kCLErrorDomain error 1.)
I also never got the prompt for allowing the app to use my location.我也从未收到允许应用程序使用我的位置的提示。
Here is the complete implementation of the LocationManager class.这是 LocationManager 类的完整实现。
class LocationManager: NSObject, ObservableObject {
private let locationManager = CLLocationManager()
@Published var region = MKCoordinateRegion.defaultRegion
static let defaultDistance: CLLocationDistance = 1000000
override init() {
super.init()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.delegate = self
}
func updateLocation() {
locationManager.requestLocation()
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
DispatchQueue.main.async {
self.region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: Self.defaultDistance, longitudinalMeters: Self.defaultDistance)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
}
I am using Xcode 13 Beta 2 and running on a simulator.我正在使用 Xcode 13 Beta 2 并在模拟器上运行。
Do add the request method:添加请求方法:
locationManager.requestWhenInUseAuthorization()
And to observe authorization status add this delegate method too:并观察授权状态也添加此委托方法:
extension ViewController: CLLocationManagerDelegate {
func locationManager(_
manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedWhenInUse:
print("Authorized When in Use")
case .authorizedAlways:
print("Authorized Always")
case .denied:
print("Denied")
case .notDetermined:
print("Not determined")
case .restricted:
print("Restricted")
@unknown default:
print("Unknown status")
}
}
}
If you are getting this error in simulator make sure simulating location is activated如果您在模拟器中遇到此错误,请确保模拟位置已激活
Target>Edit Scheme>Run>Options>Allow Location Simulation
And和
Simulator>Features>Location>City Run
In my case I added the CLLocationButton to my child view controller (which also conformed to CLLocationManagerDelegate).就我而言,我将 CLLocationButton 添加到我的子视图控制器(也符合 CLLocationManagerDelegate)。 In this setup the popup never showed up.
在此设置中,弹出窗口从未出现。
Adding the button directly to parent view controller solved the issue, popup shows up.将按钮直接添加到父视图控制器解决了问题,弹出窗口出现。
No clue why, perhaps CoreLocation framework is somehow searching for the top view controller, but the hierarchy gets broken.不知道为什么,也许 CoreLocation 框架正在以某种方式搜索顶视图控制器,但是层次结构被破坏了。 I failed to replicate the same behaviour in a simple example project.
我未能在一个简单的示例项目中复制相同的行为。 (Xcode 13 beta 5)
(Xcode 13 测试版 5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.