48

I track the user's location and ask for permission when my load first loads using this:

locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

If the user denies, but later changes their mind by enabling the configuration option in my app, how do I ask again? For example, I have a switch for auto detecting the user's location so when they enable it, I am trying to do this:

@IBAction func gpsChanged(sender: UISwitch) {
    // Request permission for auto geolocation if applicable
    if sender.on {
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
}

But this code doesn't seem to do anything. I was hoping it would ask the user again if they want to allow the app to track the user's location. Is this possible?

TruMan1
  • 33,665
  • 59
  • 184
  • 335

6 Answers6

83

The OS will only ever prompt the user once. If they deny permission, that's it. What you can do is direct the user to the Settings for your app by passing UIApplicationOpenSettingsURLString to UIApplication's openURL: method. From there, they can re-enable location services if they wish. That said, you probably shouldn't be too aggressive about bugging them for the permission.

bgilham
  • 5,909
  • 1
  • 24
  • 39
41

The permission pop-up only shows once. So we have to redirect users to Settings after that. Here comes the code in Swift:

 import CoreLocation 

 // ...

 @IBAction func userDidClickButton(_ sender: Any) {
   
    // initialise a pop up for using later
    let alertController = UIAlertController(title: "TITLE", message: "Please go to Settings and turn on the permissions", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }
        if UIApplication.shared.canOpenURL(settingsUrl) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in })
         }
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)

    alertController.addAction(cancelAction)
    alertController.addAction(settingsAction)

    // check the permission status
    switch(CLLocationManager.authorizationStatus()) {
        case .authorizedAlways, .authorizedWhenInUse:
            print("Authorize.")
            // get the user location
        case .notDetermined, .restricted, .denied:
            // redirect the users to settings
            self.present(alertController, animated: true, completion: nil)
    }
}
Allen
  • 2,979
  • 1
  • 29
  • 34
  • 3
    A quick note: If the case is `.notDetermined` then we are able to use the CLLocationManager.requestForWhenInUseAuthorization() function (as it can only be used once and `.notDetermined` means that the user has not yet decided on location permission for your app). – HirdayGupta Aug 22 '18 at 21:31
3

You can have an alternate solution!! You can show your own alert with the better message which can convince your user to allow to receive push notifications for your app. If user allows, then only you show default permission alert for enable push notification otherwise if user disallows, don't show default alert in-fact, you can save corresponding flag in your database or NSUserDefaults and can ask user later on again and again on some events in your app.

Khushabu
  • 101
  • 6
  • This solution is good but you force the user to click twice if they agree, first your popup and then the apple one. Usually you ask for permission in the beginning of the app, maybe in the first startup, maybe you have a welcome intro or a tutorial and this add an extra step. Usually Apple prefert if you keep 1 or 2 step max. – MajinDageta Jan 16 '20 at 08:52
2

You only get one chance. For the user to enable permissions after denying them, they have to go through the Settings app. See Requesting Permission to Use Location Services in CLLocationManager.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
1

I have created the library that include permission manager for notification which handle the permission alert even after user decline the permission.

https://github.com/CrazyPro007/PermissionManager/tree/master/PermissionManager/PermissionManager

TruMan1
  • 33,665
  • 59
  • 184
  • 335
CrazyPro007
  • 1,006
  • 9
  • 15
0

For the Health Kit api. Developers can simply add another permission into the readTypes Set<HKObjectType> and the user will be re-prompted to allow permissions. I'm not entirely sure if this works the same way with location services.

Harvey Connor
  • 146
  • 4
  • 19