关于iOS8无法定位 2014-10-21 11:23:14|
if (![CLLocationManager locationServicesEnabled]){
[BDUBaseToolsSpecial displayAlert:@"请开启手机定位功能!"];
return nil;
}
CLAuthorizationStatus status=[CLLocationManager authorizationStatus];
if (status==kCLAuthorizationStatusDenied){ [BDUBaseToolsSpecial displayAlert:@"请允许本程序定位"];
return nil; }即可。
升级XCode iOS8 SDK后,查看CLAuthorizationStatus这个枚举,发现多了几个选项。所以现在要这样(大概示意,具体自己修改)
CLLocationManager *locationManager=[[[CLLocationManager alloc]init]autorelease];
self.locationManager=locationManager;
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter=10.0f;
if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=8.0f)) {
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined || status==kCLAuthorizationStatusDenied || status==kCLAuthorizationStatusRestricted || status==kCLAuthorizationStatusAuthorizedWhenInUse) {
[locationManager requestAlwaysAuthorization];
} else if (status == kCLAuthorizationStatusAuthorizedAlways || status==kCLAuthorizationStatusAuthorized) {
[locationManager startUpdatingLocation];
} else {
[BDUBaseToolsSpecial displayAlert:@"定位授权状态出错,请点击取消返回上级界面"];
return; }
} else {
[locationManager startUpdatingLocation];
}然后
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:
(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
[self.locationManager requestAlwaysAuthorization];
break;
case kCLAuthorizationStatusAuthorizedAlways:
[self.locationManager startUpdatingLocation];
break;
default:
[BDUBaseToolsSpecial displayAlert:@"定位授权状态出错,请在系统设置界面中的\"隐私->定位服务\"中设置本程序权限为始终允许访问位置信息."];
break; }}
可 以根据用途,自行选择使用 [locationManager requestAlwaysAuthorization];还是 [locationManager requestWhenInUseAuthorization];查看这2个方法的注释,需要分别在app的plist文件中声明 NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription这2个 key,否则这2个方法不起作用。