0

I'm new to ios, but have the following question.

When the app starts, the user should accept the location permission. This works fine. If he doesn't allow, some features don't work.

However, I want it that when the user tries to use these features, he gets an alert that he didn't accept the location permission. This I also did. However I want the alert to have a button which will allow him to accept it now (so he doesn't have to go into settings on his phone to do it) This I couldn't manage to do.

I made a handler for the alert like this, but it doesn't work to accept the permission. Here is the handler...

 func someHandler(alert: UIAlertAction!) {
    print("User now wants to let location from new")

    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

}

Is this wrong, What else should I put it. Currently, when the handler is called the location services are not turned on.

Thanks

RJB
  • 1,704
  • 23
  • 50
  • Why are you requesting both authorizations? Only ask for the one you need. – rmaddy Feb 09 '17 at 20:26
  • Why are you requesting both location permissions? i.e. _Always_ and _WhenInUse_. You need to request for just one of them. _WhenInUse_: gets location whenever the app is in foreground whilst _Always_ accommodates both foreground and background. – eshirima Feb 09 '17 at 20:26
  • Right, fixed that, and edited my question. – RJB Feb 09 '17 at 20:26

1 Answers1

4

You can't do what you want. Only the user can change the setting by going to the Settings app.

If you could programmatically change the permission, it would be a security nightmare. And there is no API to force iOS to display the permission alert a 2nd time.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Ok. Thanks, good to know. But can I make the button go directly to the settings app page? – RJB Feb 09 '17 at 20:29
  • Yes, there are lots of examples for that. Search on `UIApplicationOpenSettingsURLString`. – rmaddy Feb 09 '17 at 20:34