iOS开发中解决iOS8无法定位的问题

升级iOS 8后,将无法定位,特给出解决方案。
1.在Plist中追加下字段NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription(这两个字段必须有其中一个,内容是系统alert的文言,文言可为空) ,如下图所示:
WhenInUse是应用在前台的时候可以搜到更新的位置信息,Always是除了应用在前台,应用在后台(suspend或terminated)都可以获取到更新的位置数据 ,根据需要,按需去申请权限。
 
2.修改代码,在self.mapView.showsUserLocation= YES;前加入如下代码

定义CLLocationManager对象,私有的或者property都可以,以确保alert弹出,用户点击完以后 CLLocationManager 对象还没被释放。示例如下:
@implementation ViewController
{
    CLLocationManager * locationManager;
}
  ……  

locationManager =[[CLLocationManager alloc] init];

  // fix ios8 location issue
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
#ifdef __IPHONE_8_0
        if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
        {
            [locationManager performSelector:@selector(requestAlwaysAuthorization)];//用这个方法,plist中需要NSLocationAlwaysUsageDescription
        }
        
        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        {
            [locationManager performSelector:@selector(requestWhenInUseAuthorization)];//用这个方法,plist里要加字段NSLocationWhenInUseUsageDescription
        }
#endif
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值