其实这个问题很简单
1、需要手动调用CLLocationManager对象的requestAlwaysAuthorization方法。
2、调用该方法需要在Info.plist中设置NSLocationAlwaysUsageDescription的字符串,这个值 (NSString *)会显示在系统提示框中 ;不要自己加值就是(Boolean),选yes;
3、在代码中添加代理方法
<pre>
/** 定位服务状态改变时调用*/
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
{
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
NSLog(@"用户还未决定授权");
break;
}
case kCLAuthorizationStatusRestricted:
{
NSLog(@"访问受限");
break;
}
case kCLAuthorizationStatusDenied:
{
// 类方法,判断是否开启定位服务
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务开启,被拒绝");
} else {
NSLog(@"定位服务关闭,不可用");
}
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
{
NSLog(@"获得前后台授权");
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"获得前台授权");
break;
}
default:
break;
}
}
网友评论