您好,登錄后才能下訂單哦!
在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后,發現定位壓根就不起作用,老是提示:
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即可定位了:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。