1.在Plist中追加下字段NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription(這兩個字段必須有其中一個,內容是系統alert的文言,文言可爲空) ,如下圖所示:
WhenInUse是應用在前臺的時候可以搜到更新的位置信息,Always是除了應用在前臺,應用在後臺(suspend或terminated)都可以獲取到更新的位置數據 ,根據需要,按需去申請權限。
2.修改代碼,在self.mapView.showsUserLocation= YES;前加入如下代碼
定義CLLocationManager對象,私有的或者property都可以,以確保alert彈出,用戶點擊完以後 CLLocationManager 對象還沒被釋放。示例如下:
@implementation ViewController
{
CLLocationManager * locationManager;
}
……
locationManager =[[CLLocationManager alloc] init];
// fix ios8 location issue
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
#ifdef __IPHONE_8_0
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[locationManager performSelector:@selector(requestAlwaysAuthorization)];//用這個方法,plist中需要NSLocationAlwaysUsageDescription
}
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager performSelector:@selector(requestWhenInUseAuthorization)];//用這個方法,plist裏要加字段NSLocationWhenInUseUsageDescription
}
#endif
}