shows user location在iOS8上不能使用地图定位

在iOS8上,使用xcode6进行地图定位时遇到错误,提示必须先请求定位授权。解决方案是在项目的XXX.info.plist文件中添加NSLocationAlwaysUsageDescription和NSLocationWhenInUseDescription键值对,并确保包含Supported interface orientations字段。代码处理方面,需要针对iOS 8及以上版本进行特殊处理。

在xcode6中 苹果地图得定位方法修改了,以前得不能用了

报错说明:Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

解决办法:

在Supporting Files下得XXX.info.plis里面添加NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseDescription 两个String字段,value可以为空,也可以设置YES,不过我得问题还是不能解决,最终还是找到得了问题所在,就是info.plist中还需要包含Supported interface orientations 这个Array字段。然后运行就解决了.


代码处理:

locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [locationManager requestWhenInUseAuthorization];
        }
        [locationManager startUpdatingLocation];

 另外一处:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (
        ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] && status != kCLAuthorizationStatusNotDetermined && status != kCLAuthorizationStatusAuthorizedWhenInUse) ||
        (![locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] && status != kCLAuthorizationStatusNotDetermined && status != kCLAuthorizationStatusAuthorized)
        ) {

        NSString *message = @"您的手机目前未开启定位服务,如欲开启定位服务,请至设定开启定位服务功能";
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"无法定位" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];

    }else {

        [locationManager startUpdatingLocation];
    }
}

上面那行是 iOS 8 以上,第二行是 iOS 7 以下, kCLAuthorizationStatusAuthorized 在 iOS 8 完全不能使用。

 別的地方:

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorized) {

    // 开始定位

}else {

    // 提示警告
}
代码修改片段来源于此:http://blog.dreambreakerx.com/tag/ios/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值