when usage tutorial requestwheninuseauthorization manager example description corelocation cllocationmanagerdelegate ios swift cllocationmanager

ios - usage - Swift LocationManager didChangeAuthorizationStatus Siempre llamado



location when in use usage description (2)

- locationManager:didChangeAuthorizationStatus: se llama poco después de que se CLLocationManager .

Puede solicitar autorización dentro del método delegado si desea:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { switch status { case .NotDetermined: locationManager.requestAlwaysAuthorization() break case .AuthorizedWhenInUse: locationManager.startUpdatingLocation() break case .AuthorizedAlways: locationManager.startUpdatingLocation() break case .Restricted: // restricted by e.g. parental controls. User can''t enable Location Services break case .Denied: // user denied your app access to Location Services, but can grant access from Settings.app break default: break } }

Tenga en cuenta que debe asignar al delegado en un asunto ''oportuno'' si desea que esto funcione.

Si de alguna manera retrasaría la asignación de delegado, por ejemplo, configurándola de forma asíncrona, podría perder la llamada inicial a - locationManager:didChangeAuthorizationStatus:

Tengo un controlador de vista que implementa el CLLocationManagerDelegate . Creo una variable de CLLocationManager:

let locationManager = CLLocationManager()

Luego, en viewDidLoad , establezco las propiedades:

// Set location manager properties locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters locationManager.distanceFilter = 50

El problema es que la función se llama incluso antes de que compruebe el estado de autorización.

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { if (status == .AuthorizedWhenInUse) { // User has granted autorization to location, get location locationManager.startUpdatingLocation() } }

¿Puede alguien informarme qué podría estar causando esto?


Swift 3

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { switch status { case .notDetermined: manager.requestAlwaysAuthorization() break case .authorizedWhenInUse: manager.startUpdatingLocation() break case .authorizedAlways: manager.startUpdatingLocation() break case .restricted: // restricted by e.g. parental controls. User can''t enable Location Services break case .denied: // user denied your app access to Location Services, but can grant access from Settings.app break } }