2015-05-07 12 views
19

Mam kontroler widoku, który implementuje CLLocationManagerDelegate. Utworzyć zmienną CLLocationManager:Swift LocationManager didChangeAuthorizationStatus Zawsze nazywany

let locationManager = CLLocationManager() 

Następnie w viewDidLoad, ustawić właściwości:

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

Problem zaczyna się, że funkcja jest wywoływana zanim jeszcze sprawdzić stan autoryzacji.

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

Czy ktoś może poinformować mnie, co może być przyczyną tego?

Odpowiedz

39

- locationManager:didChangeAuthorizationStatus: jest wywoływana krótko po zainicjowaniu CLLocationManager.

Można zażądać autoryzacji wewnątrz metody delegata, jeśli chcesz:

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 
    } 
} 

Należy pamiętać, że trzeba przypisać delegata w „odpowiednim czasie” sprawy, jeśli chcesz to do pracy.

Jeśli w jakiś sposób opóźnisz delegowanie zadań, np. ustawiając ją asynchronicznie, możesz pominąć pierwsze połączenie z numerem - locationManager:didChangeAuthorizationStatus:.

+1

Dziękuję bardzo. Nie zdawałem sobie sprawy, że zostanie wywołany po inicjalizacji. –

+0

Dzięki za odpowiedź. Dokumentacja jabłka musi być aktualizowana tymi informacjami. –

+0

Wydaje się, że masz rację, że 'didChangeAuthorizationStatus' wywołał krótko po zainicjowaniu' CLLocationManager', ale gdzie jest to udokumentowane? – bobics

5

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 
     } 
    } 
+0

Dlaczego jest napisane "Nieważna redeclaration 'locationManager (_: didChangeAuthorization :)' "Kiedy próbuję zaimplementować powyższą metodę locationManager (_: didChangeAuthorization :) w swift 4. – madu

Powiązane problemy