高德开发者论坛

标题: iOS 8无法定位的解决方案 [打印本页]

作者: chenjie_1986    时间: 2014-9-22 09:48
标题: iOS 8无法定位的解决方案
本帖最后由 chenjie_1986 于 2014-10-16 09:43 编辑

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

2.修改代码,self.mapView.showsUserLocation= YES;加入如下代码

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

  6. locationManager =[[CLLocationManager alloc] init];

  7.   // fix ios8 location issue
  8.     if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
  9. #ifdef __IPHONE_8_0
  10.         if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
  11.         {
  12.             [locationManager performSelector:@selector(requestAlwaysAuthorization)];//用这个方法,plist中需要NSLocationAlwaysUsageDescription
  13.         }
  14.         
  15.         if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
  16.         {
  17.             [locationManager performSelector:@selector(requestWhenInUseAuthorization)];//用这个方法,plist里要加字段NSLocationWhenInUseUsageDescription
  18.         }
  19. #endif
  20.     }
复制代码

以上解决方案仅针对Xcode6,适用于V2.3.0(含)之前版本。








作者: Ken_DC    时间: 2014-9-22 21:26
本帖最后由 Ken_DC 于 2014-9-22 21:27 编辑

试过了,不行啊!
没有alert弹出请求定位服务

作者: charlesleo    时间: 2014-9-23 14:24
我测试的也不行....
作者: chenjie_1986    时间: 2014-9-23 14:35
charlesleo 发表于 2014-9-23 14:24
我测试的也不行....

您好!您的locationmanager是定义成property或者私有了吗?必须保证该对象没有被释放掉。
作者: chenjie_1986    时间: 2014-9-23 14:35
charlesleo 发表于 2014-9-23 14:24
我测试的也不行....

您好!您的locationmanager是定义成property或者私有了吗?必须保证该对象没有被释放掉。
作者: Ken_DC    时间: 2014-9-23 15:48
本帖最后由 Ken_DC 于 2014-9-23 15:51 编辑

[_locationManager requestWhenInUseAuthorization];

是_locationManager成员变量。默认是strong的,应该不会被自动释放。
我也实现了代理:
  1. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  2.     switch (status) {
  3.         case kCLAuthorizationStatusDenied :
  4.         {
  5.             // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
  6.             UIAlertView *tempA = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"请在设置-隐私-定位服务中开启定位功能!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  7.             [tempA show];
  8.         }
  9.             break;
  10.         case kCLAuthorizationStatusNotDetermined :
  11.             if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
  12.                 [_locationManager requestWhenInUseAuthorization];
  13.             }
  14.             break;
  15.         case kCLAuthorizationStatusRestricted:
  16.         {
  17.             // 提示用户出错原因,可按住Option键点击 kCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
  18.             UIAlertView *tempA = [[UIAlertView alloc]initWithTitle:@"提醒"
  19.                                                            message:@"定位服务无法使用!"
  20.                                                           delegate:nil
  21.                                                  cancelButtonTitle:@"确定"
  22.                                                  otherButtonTitles:nil, nil];
  23.             [tempA show];
  24.         }
  25.             break;
  26.             
  27.         default:
  28.             break;
  29.     }
  30. }
复制代码


并且有回调。就是没有alert弹出请求定位服务
进入代理方法:kCLAuthorizationStatusNotDetermined case情况
再次调用[_locationManager requestWhenInUseAuthorization];也没反应

作者: Ken_DC    时间: 2014-9-23 15:57
找到方法了!!!!!!擦擦擦

都把这个plist属性写错了。而且一搜互联网都tmd错的。
去stackoverflow才问对。

NSLocationWhenInUseUsageDescription,不是tmd NSLocationWhenInUseDescription
作者: Ken_DC    时间: 2014-9-23 16:05
找到问题了:
请将NSLocationWhenInUseDescription 替换成 NSLocationWhenInUseUsageDescription
以讹传讹啊!!!
作者: charlesleo    时间: 2014-9-26 09:29
Ken_DC 发表于 2014-9-23 16:05
找到问题了:
请将NSLocationWhenInUseDescription 替换成 NSLocationWhenInUseUsageDescription
以讹传讹 ...

这个才是正解,已解决 . 谢谢
作者: langhua9527    时间: 2014-9-26 20:51
这个是只执行一次,还是要每个VIEWCONTROLLER都要来一下?
作者: chenjie_1986    时间: 2014-9-28 15:18
langhua9527 发表于 2014-9-26 20:51
这个是只执行一次,还是要每个VIEWCONTROLLER都要来一下?

您好!一次就行~
作者: langhua9527    时间: 2014-9-30 11:32
chenjie_1986 发表于 2014-9-28 15:18
您好!一次就行~

那我写到AppDelegete里面了
作者: hitbin    时间: 2014-10-2 17:24
你好 问一下
地理反编码 区返回,城市不返回!!!BUG啊啊,有人知道吗?
************** 更新一****************
    NSLog(@"district - %@",[LBSManager shareInstance].addressComponent.district);
    NSLog(@"district - %@",[LBSManager shareInstance].addressComponent.city);
    NSLog(@"district - %@",[LBSManager shareInstance].addressComponent.citycode);

这是打印结果
2014-10-02 17:15:38.156 SmallHorse[604:85738] district - 朝阳区
2014-10-02 17:15:38.157 SmallHorse[604:85738] district -
2014-10-02 17:15:38.157 SmallHorse[604:85738] district - 010

作者: chenjie_1986    时间: 2014-10-8 10:51
hitbin 发表于 2014-10-2 17:24
你好 问一下
地理反编码 区返回,城市不返回!!!BUG啊啊,有人知道吗?
************** 更新一********** ...

您好!直辖市——北京、上海等,逆地理编码city无返回,province返回。
作者: xinkaiaass    时间: 2014-10-14 11:14
还是不好使
作者: xinkaiaass    时间: 2014-10-14 11:58
好使了 不用2.3.0 然后用这个方法就行··
作者: ilovejuly    时间: 2014-11-20 11:26
这确实是ios8的定位的正确使用方法。
作者: MacQiQi    时间: 2015-1-26 13:22
为什么我都弄了还是不行呢?急死了
作者: MacQiQi    时间: 2015-1-26 13:22
ilovejuly 发表于 2014-11-20 11:26
这确实是ios8的定位的正确使用方法。

大神,为什么我的还是不行呢
作者: MacQiQi    时间: 2015-1-26 13:23
ilovejuly 发表于 2014-11-20 11:26
这确实是ios8的定位的正确使用方法。

为什么我的还是不能定位呢

作者: chenjie_1986    时间: 2015-1-26 13:30
MacQiQi 发表于 2015-1-26 13:23
为什么我的还是不能定位呢

用demo试试~
作者: chenjie_1986    时间: 2015-1-26 13:31
MacQiQi 发表于 2015-1-26 13:22
为什么我都弄了还是不行呢?急死了

真机还是模拟器?最好用真机~
作者: MacQiQi    时间: 2015-1-26 13:40
我的是真机,什么都改了就是不定位
作者: MacQiQi    时间: 2015-1-26 13:40
MacQiQi 发表于 2015-1-26 13:40
我的是真机,什么都改了就是不定位

- (void)locationManagerCLLocationManager *)manager didChangeAuthorizationStatusCLAuthorizationStatus)status {}
这个方法根本没有调用

作者: roysoho    时间: 2015-6-1 17:10
我没有使用地图服务,只用了搜索服务,能定位吗?
作者: amap_1511200386    时间: 2015-6-29 14:16
Ken_DC 发表于 2014-9-23 15:57
找到方法了!!!!!!擦擦擦

都把这个plist属性写错了。而且一搜互联网都tmd错的。

呵呵。
这个属性,貌似在列表里找不到。
作者: amap_1511200386    时间: 2015-6-29 14:43
Always是除了应用在前台,应用在后台(suspend或terminated)都可以获取到更新的位置数据 ,根据需要,按需去申请权限。

terminated 这个都行?
作者: chenjie_1986    时间: 2015-6-30 11:27
roysoho 发表于 2015-6-1 17:10
我没有使用地图服务,只用了搜索服务,能定位吗?

不行,定位是封装在mapview中了
作者: SafioWan    时间: 2015-7-24 18:10
Ken_DC 发表于 2014-9-23 16:05
找到问题了:
请将NSLocationWhenInUseDescription 替换成 NSLocationWhenInUseUsageDescription
以讹传讹 ...

完全按步骤来,依然定位不了,怎么破
作者: amap_1848218841    时间: 2015-8-1 11:10
SafioWan 发表于 2015-7-24 18:10
完全按步骤来,依然定位不了,怎么破

我也是完全按照步骤来,模拟器里定位不了。不过我发现自动配置出来的工程里pods只有一个target,products也只有一个.a文件,与视频演示里的不同,会是这个的问题吗?
作者: amap_1359861510    时间: 2016-1-5 16:56
Ken_DC 发表于 2014-9-23 15:57
找到方法了!!!!!!擦擦擦

都把这个plist属性写错了。而且一搜互联网都tmd错的。

你好解决了吗?我的怎么还没出来那个允许授权的 警告框




欢迎光临 高德开发者论坛 (https://lbsbbs.amap.com/) Powered by Discuz! X3.2