在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。