iOS8无法开启定位问题

升级iOS8了,突然发现定位服务无法开启。

 

于是乎网上找答案。

中文答案网上搜索一大圈,tmd都以讹传讹的用这个字段:NSLocationWhenInUseDescription
终于在stackoverflow找到答案:http://stackoverflow.com/questions/24850128/ios-8-requestwheninuseauthorization-no-popup
真正可用的字段是:NSLocationWhenInUseUsageDescription


 

以下是iOS8开启定位的具体方法:

1. 在Plist中追加下面两个字段 NSLocationWhenInUseUsageDescription, NSLocationAlwaysUsageDescription

2. 在定位前初始化一个CLLocationManager(ARC下不能为临时变量,不然不会弹出请求定位的弹窗),并设置好代理。

3.然后实现代理:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        case kCLAuthorizationStatusDenied :
        {
            // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
            UIAlertView *tempA = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"请在设置-隐私-定位服务中开启定位功能!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [tempA show];
        }
            break;
        case kCLAuthorizationStatusNotDetermined :
//根据自己的需求和plist中的字段调用requestWhenInUseAuthorization或requestAlwaysAuthorization
            if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [_locationManager requestWhenInUseAuthorization];
            }
            break;
        case kCLAuthorizationStatusRestricted:
        {
            // 提示用户出错原因,可按住Option键点击 kCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
            UIAlertView *tempA = [[UIAlertView alloc]initWithTitle:@"提醒"
                                                           message:@"定位服务无法使用!"
                                                          delegate:nil
                                                 cancelButtonTitle:@"确定"
                                                 otherButtonTitles:nil, nil];
            [tempA show];
        }
            break;
            
        default:
            break;
    }
}
View Code
原文地址:https://www.cnblogs.com/bunsman/p/3988653.html