Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用objective-c在ios 9中尝试在不提示位置授权的情况下启动MapKit位置更新_Ios_Objective C_Ios9 - Fatal编程技术网

使用objective-c在ios 9中尝试在不提示位置授权的情况下启动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. 在这里查找时,我发

我正在尝试在iOS 8上使用MapKit,但不断出现错误:

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;
    }
}