標籤:ios8 定位 問題
在ios7之前,我們進入程式提示使用者開啟定位是這樣做的,如下:
CLLocationManager * locationManager = [[CLLocationManager alloc] init];//建立位置管理器
//locationManager.delegate=_instance;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=100.0f;
//定位服務是否可用
BOOL enable=[CLLocationManager locationServicesEnabled];
//是否具有定位許可權
int status=[CLLocationManager authorizationStatus];
if(!enable || status<3){
//請求許可權
[locationManager requestWhenInUseAuthorization];
}
但是到了ios8後,發現定位壓根就不起作用,老是提示:650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4B/F2/wKioL1Q1_3Gwxq89AAEJ-etU45o752.jpg" title="016C4C99-9E8C-4FB3-A14E-F08152804E06.png" alt="wKioL1Q1_3Gwxq89AAEJ-etU45o752.jpg" />
ios8定位解決如下:
先在info.plist定義key:(總是授權)NSLocationAlwaysUsageDescription或者使用時授權NSLocationWhenInUseUsageDescription
在.h裡面
繼承代碼CLLocationManagerDelegate
定義CLLocationManager *_locationManager;
在.m裡面定義:
- (void)startTrackingLocation {
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined) {
//總是授權
[_locationManager requestAlwaysAuthorization];
//每次授權一次
//[_locationManager requestWhenInUseAuthorization];
}
else if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
[_locationManager startUpdatingLocation];
}
}
//實現代碼回調
#pragma mark - CLLocationManager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"Got authorization, start tracking location");
[self startTrackingLocation];
break;
case kCLAuthorizationStatusNotDetermined:
[_locationManager requestAlwaysAuthorization];
break;
default:
break;
}
}
調用如下:
if (IOS8) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self startTrackingLocation];
}
最後實現效果如,點擊Allow即可定位了:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4B/F5/wKioL1Q2AjLh6ra9AAEmwOdLAiE207.jpg" title="1EA3D9CE-DECD-4F23-A751-DDC6FF68C217.png" alt="wKioL1Q2AjLh6ra9AAEmwOdLAiE207.jpg" />
IOS8上定位問題