18

I want to have the same functionality as the Map app, where user is prompted every time they press the 'current location' button to turn on their Location Services if they are off:

  • Turn off location services
  • User presses 'getCurrentLocation' button
  • App tries to get location using CLLocationManager
  • User gets 'Turn On Location Services..." message that shows "Settings" and "Cancel" buttons.
  • User taps 'Cancel'
  • User presses ''getCurrentLocation' button again
  • App tries to get location using CLLocationManager again
  • User does not get 'Turn On Location Services..." message any more

In the Map app, the user gets "Turn On Location Services..." message every time. How can I get my app to do the same? I made user I am using a new instance of CLLocationManager, in case that was the problem, but it was not. I can't see any settings that would affect this.

If I make my own Alert I cannot get the same 'Settings' button functionality. Also, I don't want the user to see multiple Alerts that look the same.

Any ideas?

Anomie
  • 92,546
  • 13
  • 126
  • 145
toofah
  • 4,455
  • 4
  • 31
  • 42
  • So the functionality we see in the maps application cannot be replicated in our own apps is the conclusion of it all? No solution it seems? Any updates? I'm working on an iOS 7 app... – John Erck Oct 10 '13 at 03:58

3 Answers3

22

New in iOS 8 there is a constant called UIApplicationOpenSettingsURLString.

From the "What's new in iOS" document under UIKit is the line:

You can take the user directly to your app-related settings in the Settings app. Pass the UIApplicationOpenSettingsURLString constant to the openURL: method of the UIApplication class.

From Apple's documentation:

UIApplicationOpenSettingsURLString

Used to create a URL that you can pass to the openURL: method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.

You can pass this into the UIApplication openURL: method. It might look something like:

NSURL *settings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

if ([[UIApplication sharedApplication] canOpenURL:settings])
    [[UIApplication sharedApplication] openURL:settings];
Community
  • 1
  • 1
Aaron Wasserman
  • 1,249
  • 11
  • 8
19

If you want to point the user back to the Location Services screen in the Settings app, you can do so by sending them to a special URL, like so:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"prefs:root=LOCATION_SERVICES"]];
Jacob White
  • 365
  • 3
  • 8
  • 3
    Apparently not anymore. (http://stackoverflow.com/questions/9627451/how-to-open-preferences-settings-with-ios-5-1) – Matthew Lowe Sep 25 '12 at 19:00
10

You can query the shared CLLocationManager instance if the location service is enabled. The correct way is to respect the users choice to disable location services.

But if you want to, just start the location service anyway and the user will be prompted to start it again. If the user opts in on the request locations will begin to be reported on your delegate as usual. If the user instead denies your request you will get a failure callback to the locationManager:didFailWithError: delegate method. The error will have an error code of kCLErrorDenied.

I would strongly discourage you from doing this, but you can try to start the service again if the user says no, and the user will be asked again. Most users will hate you for it though.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
  • +1 the user should only decide whether they want to allow an app to use their location at one point, ive noticed a lot of apps ive used recently request permission regularly, even if it hasnt been allowed in the past, which im not a fan of – Matt May 13 '11 at 23:02
  • agree. if your app relies on location services for any significant functionality, the nice way to do this is to detect when location services are disabled and present passive UI in your app that notifies users and allows them to turn location services back on. If a user opts in you can then start location services which will prompt the user to enable. – XJones May 14 '11 at 00:29
  • 2
    Thanks for your feedback. I am with you and totally do not want to annoy my users be re-asking them to allow my application access to location services. But in this case, the user is tapping on the "get current location" button, so it seems appropriate to tell them that they need to turn this on. This is the same as in the Map application when the user taps on the "get current location" button. I am doing as you suggest, calling the start monitor method, but it does not re-prompt the user, I just get the kCLErrorDenied error. – toofah May 14 '11 at 17:28
  • 27
    This is incorrect. Once the location services authorization alert view is presented, users will *never* see it again. Ever. (Notwithstanding a phone reset.) Even an app delete and reinstall will not present it again. The only way a user can restore location service authorization is to go to the Settings app and re-enable it directly. – Yetanotherjosh Aug 11 '11 at 21:21
  • 3
    @Yetanotherjosh additionally, location warnings can be reset manually in Settings > General > Reset > Reset Location Warnings. – MattyG Nov 12 '11 at 03:30
  • @MattyG indeed, I saw this recently myself. Appears to have been added in recent OS updates, or have I missed it along? Either way, it's handy. Also note that now in iOS 5, deleting and reinstalling an app will reset all of these types of permission questions, so indeed my "never, ever" has now been invalidated on multiple counts. – Yetanotherjosh Nov 14 '11 at 10:00