iOS8 Location: How should one request Always Autho

2020-03-18 03:59发布

When my app launches the map view, I request the iOS8 "When In Use" location permission. Assume user grants that.

I would like to request the Always permission only when user opts-in to my geofencing feature. But calling CLLocationManager.requestAlwaysAuthorization has no effect because the current authorization status is no longer kCLAuthorizationStatusNotDetermined.

How would one go about requesting the Always permission AFTER user has granted When In Use permission? I would think this is a common use case because apps should avoid asking for the Always permission unless needed.

2条回答
闹够了就滚
2楼-- · 2020-03-18 04:55

You are right, calling requestAlwaysAuthorization will not do anything if the user already granted 'when in use' permission. A workaround I used was to link the user to the settings screen and let them turn on the 'Always' setting themselves. Here are the steps to do that:

  1. Create a new key in your app-Info.plist called NSLocationAlwaysUsageDescription and enter some reasons as to why you need to request the always permission in the value field.

    info.plist file

  2. Link your user to your app's settings screen (more info here)

    NSURL *settings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:settings])
        [[UIApplication sharedApplication] openURL:settings];
    
  3. Once the user taps your link they will see this:

    Initial settings screen

    and when they click on Location, they will be able to see both While Using the App and Always settings to choose from:

    Location access settings screen

  4. Monitor authorization changes in your app by implementing the CLLocationManager delegate method locationManager:didChangeAuthorizationStatus:
查看更多
我想做一个坏孩纸
3楼-- · 2020-03-18 04:59

I don't know about objective-c, but it works fine for me in swift and iOS 8.4. Make sure you provide both keys in your info.plist

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
// iOS 11 and up will require this key instead of AlwaysUsageDescription
NSLocationAlwaysAndWhenInUsageDescription

Then just call

locationManager.requestAlwaysAuthorization() 

And make sure locationManager is an instance variable! A local variable will be ignored for some strange reason. Apple Documentation

查看更多
登录 后发表回答