简体   繁体   中英

iOS Location Services asks on app open

Just trying to add location services to my app (in ObjC). I only want the permissions prompt to appear when I am about to use it, however the prompt appears when I first open the app.

My impression was that the prompt would only appear when startUpdatingLocation was called on the CLLocationManager object. However this is only called in my app on certain ViewControllers (definitely not my first view controller).

Interestingly this issue only appears in iOS8, it's fine in iOS7. Anyone had a similar issue? Or have any idea what else could be wrong?

Thanks.

iOS 8 changes how location services are handled, and it also means that if you're supporting older versions of the OS you potentially need to do some extra coding. For an wonderfully detailed look at the entire process, checkout NSHipster

Prior to iOS 8, yes calling startUpdatingLocation would prompt the user for permission. Though now requesting permission and starting the location updates are separate and you'll need to call requestWhenInUseAuthorization or requestAlwaysAuthorization .

Not only that, but in iOS8 you need to explicitly add two plist keys and then define their values: NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription , which are strings that will show up in the alert that notifies users that your app is requesting location information.

But seriously, check out the link. It's phenomenal at explaining this all, far better than I could summarize here.

Without knowing where you had placed your code, the way I would approach this issue is

  • Create a method that asks for location access ie - (void) findLocationAsk
  • Now don't call this method right on app launch as you didn't want that behavior
  • Call this method in your view controller
  • You can call it in your viewDidLoad or as a button action or fire it as a timer.

This should take care of your prompt on app launch

UPDATE: Your app will prompt for location access right on startup on iOS 8. This is because the "Required background modes" required key is detected in plist file. The only thing you can do is make that message more descriptive as to why you need access. Add this key and description pair to your plist file and add whatever explanation text you like in it

NSLocationWhenInUseUsageDescription Please grant this app access to your location incase of failed login attempts are detected and you want to know who it was

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM