对于iPhone上的最新iOS版本,我不会始终在iPhone上跟踪您的位置。我会在应用程序上,从来没有。但是,此功能似乎对以前的每个版本都可以正常工作。除了我在下面的Xcode中所做的任何其他建议都将很棒。
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.");
}
在PLIST文件中
<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>
代码具有逻辑倒置,应该是:
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.");
}
注意我切换了这两条线:
[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
您可以参考主源代码。
此外,对于iOS 11,当请求始终许可时,您应将NSLocationAlwaysAndWhenInUseUsageDescription
和NSLocationWhenInUseUsageDescription
密钥包括在您的信息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>
您必须在应用程序的info.plist文件中包含NSLocationWheninUseusagedEsagedEscription和NslocationalSagideScription和NslocationalSevelways和wheninuseusateDescription键。(如果您的应用程序支持iOS 10及更早,则还需要NSlocationalwaysingescription键。)如果不存在这些键,则授权请求立即失败。