When does an iphone application receive didChangeA

2019-04-06 13:18发布

问题:

I have a question about CLLocationManagerDelegate. The documentation says if the user changes the settings for your location services (in the iPhone's Settings.app) then your app is supposed to receive an didChangeAuthorizationStatus: message to the delegate. My question is, when would this happen?

If the user changed the setting, it means they are in the settings app, and your app is either backgrounded or not running at all, so in the former case, when would your app's CLLocationManager delegate get the didChangeAuthorizationStatus: call?

回答1:

I just ran across this method an hour ago, so good timing on the question!

It looks like in my case this method gets called:

  1. When the app becomes active.
  2. On allowing Location Services for the app on the initial startup of the app.

I wrote a quick test app you can find here:

https://github.com/mharper/LocationServices

It simply logs the authorization status whenever the method gets called.



回答2:

This delegate method will be called when:

  1. The first time you init a CLLocationManager instance

    1.1. If that's the first time your App launch on device, you'll receive state kCLAuthorizationStatusNotDetermined before user see the [Allow/Don't Allow] UIAlertView. (At this time, you can find that the UISwitch of your App in Settings - Privacy - Location Service is turn off or not shown.

    1.2. When user re-launch your App. Because decision has been made in previous launch, so you can retrieve the state.

  2. The first time after user made decision. This is obvious. After you call the startUpdatingLocation/startUpdatingHeading, then iOS show the UIAlertView.

  3. In your answer's situation, user made changes in Settings, if your App is running in background, you'll receive the new state when your App become active. Otherwise, reference 1.2.



回答3:

If your app is running in the background or not at all it will be called the moment the user returns to your application.



回答4:

I encounter the same issue as well.

my solution is put the request gps authorization code in main loop.

I guess it can also works if you put in another run loop.



回答5:

I ran into this problem of handling location permission changes correctly recently and did a lot of research and debugging.

If the app is not running at all and the user changes location permissions in Settings, then when you start your app, locationManager:didChangeAuthorizationStatus is called when the location manager is initialized as stated in mharper's answer. This behavior is not in Apple's docs.

If the app is in the background/suspended, I tested it in the simulator and it looks like the delegate function is also called.