今天修改的一個項目bug葵第,在IOS 8 的simulator中運(yùn)行app绘迁,就會卡死在定位的頁面。原因是在 iOS 7 以及更早之前的版本卒密,MapView 顯示使用者位置不需作到 CLLocationManager缀台,現(xiàn)在都要了。
在 iOS 8 上編譯會出現(xiàn)以下 log :
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
經(jīng)搜索得到解決方法如下:
1哮奇、修改info.plist:
新增key
值為NSLocationWhenInUseUsageDescription
或 NSLocationAlwaysUsageDescription
(這里膛腐,我將兩個都加了進(jìn)去),value
可以為空鼎俘,也可以設(shè)置YES
哲身,不過我得問題還是不能解決,最終還是找到得了問題所在贸伐,就是info.plist
中還需要包含Supported interface orientations
這個Array
字段勘天。然后運(yùn)行就解決了。
2、修改代碼:
在調(diào)用方法startUpdatingLocation
的前面加上一句
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
3脯丝、博客原文修改方法:
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 = @"您的手機(jī)目前並未開啟定位服務(wù)商膊,如欲開啟定位服務(wù),請至設(shè)定->隱私->定位服務(wù)宠进,開啟本程式的定位服務(wù)功能";
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 {
// 顯示警告
}