Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location permission dialog is shown and immediately disappears

The iOS dialog prompts and disappears after half a second:

        let locationManager = CLLocationManager()
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            print("In Use \(locationManager.location?.description)")
        case .denied, .restricted:
            print("denied")
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .authorizedAlways:
            print("always \(locationManager.location)")
        }

I don't know if this is relevant, but I'm using SWReavealViewController. Xcode9, compiled for iOS 8.0, both simulator and real device

like image 569
gutte Avatar asked Oct 19 '17 17:10

gutte


2 Answers

Your locationManager variable won't live beyond the scope of its definition (the function where that snippet of code lives), so it is deallocated before the user can respond to the dialog.

If you move let locationManager = CLLocationManager() up to a class variable, it should stick around.

like image 147
Justin Anderson Avatar answered Nov 15 '22 03:11

Justin Anderson


Insert let locationManager = CLLocationManager() to the very beginning of the your ViewController class. (Outside of viewDidLoad() function). Hope it helps you.

class ViewController: UIViewController {

  let locationManager = CLLocationManager()

  override func viewDidLoad() {
    super.viewDidLoad()
  }
}
like image 39
Mahyar Avatar answered Nov 15 '22 05:11

Mahyar