IOS开发—App 在 IOS 8 的simulator运行时,定位卡死bug解决

在 iOS 8 上编译会出现如下 log : spa

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first. code

经搜索获得解决方法以下: ip

一、修改info.plist: 博客

    新增key值为NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription(这里,我将两个都加了进去),value能够为空,也能够设置YES,不过我得问题仍是不能解决,最终仍是找到得了问题所在,就是info.plist中还须要包含Supported interface orientations 这个Array字段。而后运行就解决了。 it

二、修改代码: io

    在调用方法startUpdatingLocation的前面加上一句 编译

if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [locationManager requestWhenInUseAuthorization];
}
三、博客原文修改方法:

locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [locationManager requestWhenInUseAuthorization];
        }
        [locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (
        ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] && status != kCLAuthorizationStatusNotDetermined && status != kCLAuthorizationStatusAuthorizedWhenInUse) ||
        (![locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] && status != kCLAuthorizationStatusNotDetermined && status != kCLAuthorizationStatusAuthorized)
        ) {

        NSString *message = @"您的手機目前並未開啟定位服務,如欲開啟定位服務,請至設定->隱私->定位服務,開啟本程式的定位服務功能";
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"無法定位" message:message delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
        [alertView show];

    }else {

        [locationManager startUpdatingLocation];
    }
}
上面那行是 iOS 8 以上,第二行是 iOS 7 如下,因為 kCLAuthorizationStatusAuthorized 在 iOS 8 彻底不能使用。
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorized) {

    // 開始定位

}else {

    // 顯示警告
}
相关文章
相关标签/搜索