在ios7之前,我們進(jìn)入程序提示用戶開啟定位是這樣做的,如下:
10年積累的網(wǎng)站建設(shè)、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有肇源免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
CLLocationManager * locationManager = [[CLLocationManager alloc] init];//創(chuàng)建位置管理器
//locationManager.delegate=_instance;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=100.0f;
//定位服務(wù)是否可用
BOOL enable=[CLLocationManagerlocationServicesEnabled];
//是否具有定位權(quán)限
int status=[CLLocationManager authorizationStatus];
if(!enable || status<3){
//請求權(quán)限
[locationManager requestWhenInUseAuthorization];
}
但是到了ios8后,發(fā)現(xiàn)定位壓根就不起作用,老是提示:
ios8定位解決如下:
先在info.plist定義key:(總是授權(quán))NSLocationAlwaysUsageDescription或者使用時授權(quán)NSLocationWhenInUseUsageDescription
在.h里面
繼承代碼CLLocationManagerDelegate
定義CLLocationManager *_locationManager;
在.m里面定義:
- (void)startTrackingLocation {
CLAuthorizationStatus status = [CLLocationManagerauthorizationStatus];
if (status ==kCLAuthorizationStatusNotDetermined) {
//總是授權(quán)
[_locationManagerrequestAlwaysAuthorization];
//每次授權(quán)一次
//[_locationManager requestWhenInUseAuthorization];
}
elseif (status ==kCLAuthorizationStatusAuthorizedWhenInUse || status ==kCLAuthorizationStatusAuthorizedAlways) {
[_locationManagerstartUpdatingLocation];
}
}
//實現(xiàn)代碼回調(diào)
#pragma mark - CLLocationManager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
casekCLAuthorizationStatusAuthorizedAlways:
casekCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"Got authorization, start tracking location");
[selfstartTrackingLocation];
break;
casekCLAuthorizationStatusNotDetermined:
[_locationManagerrequestAlwaysAuthorization];
break;
default:
break;
}
}
調(diào)用如下:
if (IOS8) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate =self;
[selfstartTrackingLocation];
}
最后實現(xiàn)效果如下圖,點擊Allow即可定位了: