简体   繁体   中英

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:

在此处输入图片说明

Now, when I use the following code to call updateLocation I get the following error:

[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.

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.

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). 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. I failed to replicate the same behaviour in a simple example project. (Xcode 13 beta 5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM