iOS開發中解決iOS8無法定位的問題

升級iOS 8後,將無法定位,特給出解決方案。
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
    }
發佈了21 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章