对于iPhone上的最新ios版本,我没有得到“永远不会”的#i;始终在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>
答案 0 :(得分:1)
代码的逻辑是反转的,应该是这样的:
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];
您可以参考主人source code。
此外,对于iOS 11,requesting always permission时,您应在信息plist中加入NSLocationAlwaysAndWhenInUseUsageDescription
和NSLocationWhenInUseUsageDescription
键。
<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文件中包含NSLocationWhenInUseUsageDescription和NSLocationAlwaysAndWhenInUseUsageDescription键。 (如果您的应用支持iOS 10及更早版本,则还需要NSLocationAlwaysUsageDescription键。)如果这些键不存在,则授权请求会立即失败。