您好,登錄后才能下訂單哦!
在ios7之前,我們進(jìn)入程序提示用戶開啟定位是這樣做的,如下:
CLLocationManager * locationManager = [[CLLocationManager alloc] init];//創(chuàng)建位置管理器
//locationManager.delegate=_instance;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=100.0f;
//定位服務(wù)是否可用
BOOL enable=[CLLocationManager locationServicesEnabled];
//是否具有定位權(quán)限
int status=[CLLocationManager authorizationStatus];
if(!enable || status<3){
//請(qǐng)求權(quán)限
[locationManager requestWhenInUseAuthorization];
}
但是到了ios8后,發(fā)現(xiàn)定位壓根就不起作用,老是提示:
ios8定位解決如下:
先在info.plist定義key:(總是授權(quán))NSLocationAlwaysUsageDescription或者使用時(shí)授權(quán)NSLocationWhenInUseUsageDescription
在.h里面
繼承代碼CLLocationManagerDelegate
定義CLLocationManager *_locationManager;
在.m里面定義:
- (void)startTrackingLocation {
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined) {
//總是授權(quán)
[_locationManager requestAlwaysAuthorization];
//每次授權(quán)一次
//[_locationManager requestWhenInUseAuthorization];
}
else if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
[_locationManager startUpdatingLocation];
}
}
//實(shí)現(xiàn)代碼回調(diào)
#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;
}
}
調(diào)用如下:
if (IOS8) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self startTrackingLocation];
}
最后實(shí)現(xiàn)效果如下圖,點(diǎn)擊Allow即可定位了:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。