使用objective-c在ios 9中尝试在不提示位置授权的情况下启动MapKit位置更新
我正在尝试在iOS 8上使用MapKit,但不断出现错误:使用objective-c在ios 9中尝试在不提示位置授权的情况下启动MapKit位置更新,ios,objective-c,ios9,Ios,Objective C,Ios9,我正在尝试在iOS 8上使用MapKit,但不断出现错误: Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first. 在这里查找时,我发
Trying to start MapKit location updates without prompting for location authorization. Must call
-[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager
requestAlwaysAuthorization] first.
在这里查找时,我发现我必须在plist中的usagedescription中实现nslocation,但什么都没有发生,我仍然在控制台中得到该错误。我做错了什么?只需在
CLLocationManager
初始化中添加这些代码
例如:
@property(强,非原子)CLLocationManager*locationManager代码>
然后在您的实现中,只需调用[self.locationManager requestwhenuseauthorization]调用更新函数之前的代码>。在您的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];
}
参考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>
3.-当用户接受时更新位置
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
}
}
除了实现PList中的键(我有nslocationwhenUsageDescription
和隐私-位置使用说明
),我用以下代码解决了警告:
if ([CLLocationManager locationServicesEnabled])
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusNotDetermined) {
} else {
_mapView.showsUserLocation = YES;
}
}
if ([CLLocationManager locationServicesEnabled])
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusNotDetermined) {
} else {
_mapView.showsUserLocation = YES;
}
}