When does an iphone application receive didChangeA

2019-04-06 13:13发布

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?

5条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-06 13:25

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.

查看更多
Root(大扎)
3楼-- · 2019-04-06 13:26

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-04-06 13:32

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.

查看更多
Viruses.
5楼-- · 2019-04-06 13:33

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.

查看更多
唯我独甜
6楼-- · 2019-04-06 13:44

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

查看更多
登录 后发表回答