-1

I am using the instance method requestWhenInUseAuthorization() of the CLLocationManager class to force the alert message Turn On Location Services to Allow "myApp" to Determine Your Location to appear a second time after the user selects Cancel on its first appearance but the alert message does not appear a second time. Can anyone explain where I am going wrong in my code? Thanks.

I have the code below in one custom sub-class of UITableViewController;

if (CLLocationManager.locationServicesEnabled() == false) || (CLLocationManager.authorizationStatus() != .authorizedWhenInUse)
{

    self.navigationController!.popViewController(animated: true)

}

which should force the previous of instance of another custom sub-class of UITableViewController to display its view again in its viewDidAppear() method below;

    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)

    // prompts user to turn on location services
    locationManager.requestWhenInUseAuthorization()

}

but it doesn't result in the alert message being shown to the user a second time. Both custom sub-class instances of UITableViewController are embedded in a UINavigationController instance in my storyboard file.

Muske753
  • 9
  • 2
  • The alert is shown once. After that you need to write app specific code that checks and reminds the user to change things in `Settings` if they want to. – Magnas Mar 09 '19 at 17:09

2 Answers2

1

It's only possible to ask the user for permission to use location services once. If the user declines, it isn't possible to prompt them again. From the documentation of requestWhenInUseAuthorization():

When the current authorization status is notDetermined, this method runs asynchronously and prompts the user to grant permission to the app to use location services.

If the current authorization status is anything other than notDetermined, this method does nothing and does not call the locationManager(_:didChangeAuthorization:) method.

For the user to give your application location permission after they've declined it, they need to go to the Settings app on their device, find your app, then enable "Location" permissions there. You can make this easier for the user by providing a link in your application to this settings page by opening the UIApplicationOpenSettingsURLString URL.

If you haven't already, take a look at the "Requesting Permission" section of the Human Interface Guidelines, for best practises around this.

Defragged
  • 1,914
  • 15
  • 15
  • OK, thanks @Defragged. I have now included some additional code to deal with this situation which works; – Muske753 Mar 09 '19 at 22:11
  • OK, thanks @Defragged. I have now included some additional code to deal with this situation which works. However I now have the problem forcing the second alert message to appear for the user `Allow "myApp" to access your location while you are using the app?`. Calling the `requestWhenInUseAuthorization()` method of the first custom sub-class instance of `UITableViewController` does not force the alert to be displayed to the user. – Muske753 Mar 09 '19 at 22:20
  • If the user has ever been shown the location prompt for your app, they will never see it again unless they delete and reinstall your app. It’s an intentional limit built into iOS. There’s nothing you can do to circumvent this. – Defragged Mar 09 '19 at 23:26
0

You don't need the system alert. You can find out whether location services is enabled, by calling (wait for it) locationServicesEnabled(). And then you can present an alert begging the user to turn it on.

matt
  • 515,959
  • 87
  • 875
  • 1,141