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..net

經搜索獲得解決方法以下:code

一、修改info.plist:orm

    新增key值爲NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription(這裏,我將兩個都加了進去),value能夠爲空,也能夠設置YES,不過我得問題仍是不能解決,最終仍是找到得了問題所在,就是info.plist中還須要包含Supported interface orientations 這個Array字段。而後運行就解決了。 blog

二、修改代碼: ip

    在調用方法startUpdatingLocation的前面加上一句 ci

?get

1
2
3
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
     [locationManager requestWhenInUseAuthorization];
}

三、博客原文修改方法: 博客

?it

1
2
3
4
5
6
7
locationManager = [[CLLocationManager alloc] init];
         locationManager.delegate = self;
         locationManager.desiredAccuracy=kCLLocationAccuracyBest;
         if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
             [locationManager requestWhenInUseAuthorization];
         }
         [locationManager startUpdatingLocation];

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- ( 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 徹底不能使用。 

?

1
2
3
4
5
6
7
8
9
10
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
     
     if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorized) {
 
     // 開始定位
 
} else {
 
     // 預示警告
}
相關文章
相關標籤/搜索