我试图在iOS 8上使用MapKit并且我一直收到错误:
Trying to start MapKit location updates without prompting for location authorization. Must call
-[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager
requestAlwaysAuthorization] first.
在这里查看,我发现我必须在我的plist中实现NSLocationWhenInUsageDescription,但没有任何反应,我仍然在控制台中得到错误。我做错了什么?
答案 0 :(得分:2)
1.-将以下行添加到info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>
2.-提示位置授权:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
3. - 用户接受后更新位置...
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
}
}
答案 1 :(得分:0)
只需在CLLocationManager
初始化中添加这些代码。
例如:
@property (strong, nonatomic) CLLocationManager *locationManager;
然后在您的实现中,只需在调用更新函数之前调用[self.locationManager requestWhenInUseAuthorization];
。
答案 2 :(得分:0)
在cllocationmanager
初始化检查ios 8后,并要求获得许可
locationManager = [[CLLocationManager alloc] init];// [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
参考http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
答案 3 :(得分:0)
除了在PList中实现密钥(我有NSLocationWhenInUseUsageDescritpion
和Privacy - Location Usage Description
)之外,我还使用以下代码解决了警告:
if ([CLLocationManager locationServicesEnabled])
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusNotDetermined) {
} else {
_mapView.showsUserLocation = YES;
}
}