我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
当前回答
// ** Don't forget to add NSLocationWhenInUseUsageDescription in MyApp-Info.plist and give it a string
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
// Location Manager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", [locations lastObject]);
}
其他回答
要在iOS 8中访问用户位置,你必须添加,
NSLocationAlwaysUsageDescription in the Info.plist
这将询问用户获取其当前位置的权限。
Swift开发者常犯的一个错误:
首先确保你添加了一个值到plist为NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription。
如果你仍然没有看到一个窗口弹出要求授权,看看你是否把行var locationManager = CLLocationManager()在你的视图控制器的viewDidLoad方法。如果这样做,那么即使调用locationManager.requestWhenInUseAuthorization(),也不会显示任何内容。这是因为在viewDidLoad执行后,locationManager变量被释放(清除)。
解决方案是在类方法的顶部找到var locationManager = CLLocationManager()行。
保持可可钥匙信息总是在你的指尖为这些更新,这里是链接:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26
享受。
对我来说,问题是CLLocationManagerDelegate类是私有的,这阻止了所有的委托方法被调用。我想这不是很常见的情况,但我想我应该提到它,以防它能帮助到任何人。
苹果的文件显示:
https://developer.apple.com/documentation/corelocation/requesting_permission_to_use_location_services https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization
As of iOS 8, the presence of a NSLocationWhenInUseUsageDescription or a NSLocationAlwaysUsageDescription key value in your app's Info.plist file is required. It's then also necessary to request permission from the user prior to registering for location updates, either by calling [self.myLocationManager requestWhenInUseAuthorization] or [self.myLocationManager requestAlwaysAuthorization] depending on your need. The string you entered into the Info.plist will then be displayed in the ensuing dialog.
如果用户授予了权限,则业务照常进行。如果他们拒绝权限,则委托不会被告知位置更新。