標籤:des style blog http io ar os sp 檔案
CLLocationManager這個系統定位的類在iOS8之前要實現定位,只需要遵守CLLocationManagerDelegate這個代理即可:
- (void)startLocate
{
if([CLLocationManager locationServicesEnabled]){
_locManager = [[CLLocationManager alloc]init];
[self.locManager setDelegate:self];
[self.locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locManager startUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[_locManager stopUpdatingLocation];
CLLocation *currentLocation = [locations lastObject];
CLLocationCoordinate2D coor = currentLocation.coordinate;
NSString *latitude = @(coor.latitude).description;
NSString *longitude = @(coor.longitude).description;
}
iOS8之前以上兩個方法正常的情況下,已經能夠正常擷取經緯度了。但是在iOS8下,則需要在startLocate這個方法中在CLLocationManager這個類執行個體化後添加如下代碼:
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[ self.locManager requestAlwaysAuthorization];
}
但是僅僅添加以上代碼還不行,還需要在工程的info.plist檔案中添加索引值對:
Key Type Value
NSLocationAlwaysUsageDescription Array/Dictionary/Boolean/Data/Date/Number/String
注意上方對應的Type,這七種類型都支援,我重點說說String和Boolean類型:
1.Type為String時:Value這個地方開發人員可以根據需要說明定位的具體目的,讓使用者清楚的知道開啟定位後用於做什麼,更加透明化,清晰化;
彈框中"定位"兩字即為Type為String時Value設定的值為"定位"時的展示。
當Value的值為空白時,彈框顯示如下:
2.Type為Boolean時:Value填YES即可。彈框顯示如下:
而iOS8以前,定位提示的彈框顯示如下:
在iOS8下如果不做以上兩處的處理,系統CLLocationManager這個類即時指定了代理,其擷取經緯度的代理方法也不會走。
系統定位在iOS8中的改變