Reputation: 983
For the latest ios version on the iPhone, I'm not getting the 'always' for always track your location on the iPhone. I'm getting while on the app and never. However this functionality seems to be working fine for every version previous. Any other suggestions than what I've done in XCode below would be great.
CDVLocation.m
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[self.locationManager requestWhenInUseAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestAlwaysAuthorization];
} else {
NSLog(@"[Warning] No NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key is defined in the Info.plist file.");
}
in the plist file
<key>NSLocationAlwaysUsageDescription</key>
<string>This app requires constant access to your location in order to track your position, even when the screen is off.</string>
Upvotes: 0
Views: 1221
Reputation: 6622
The code has the logic inverted, it should be this:
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[self.locationManager requestAlwaysAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
NSLog(@"[Warning] No NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key is defined in the Info.plist file.");
}
Notice I switched these two lines:
[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
You can refer to the master source code.
Additionally, for iOS 11, when requesting always permission you should include the NSLocationAlwaysAndWhenInUseUsageDescription
and the NSLocationWhenInUseUsageDescription
key in your info plist.
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>When always is requested in iOS 11</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>When "when in use" is requested in iOS 11</string>
You are required to include the NSLocationWhenInUseUsageDescription and NSLocationAlwaysAndWhenInUseUsageDescription keys in your app's Info.plist file. (If your app supports iOS 10 and earlier, the NSLocationAlwaysUsageDescription key is also required.) If those keys are not present, authorization requests fail immediately.
Upvotes: 1