iOS 8定位问题(转)

iOS8 定位问题

转载自:  http://www.th7.cn/Program/IOS/201409/282090.shtml

在IOS8中定位功能新增了两个方法:

- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);


这两个新增的方法导致,之前写的程序在iOS8运行会出现,定位功能无法正常使用

这样让iOS8正常使用定位功能呢?

<1>你需要在info.plist表里面添加两条变量

在Info.plist中加入两个缺省没有的字段

  • NSLocationAlwaysUsageDescription

  • NSLocationWhenInUseUsageDescription

这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语。


这样在写代码:

    CLLocationManager  *locationManager = [[CLLocationManager alloc]init];    locationManager.delegate = self;    [locationManager requestAlwaysAuthorization];    locationManager.desiredAccuracy = kCLLocationAccuracyBest;    locationManager.distanceFilter = kCLDistanceFilterNone;    [locationManager startUpdatingLocation];


这是在调用代理

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {    switch (status) {        case kCLAuthorizationStatusNotDetermined:            if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {                [locationManager requestWhenInUseAuthorization];            }            break;        default:            break;    }}


这样就Ok了,就会弹出原来的提示框

今天在开发的时候发现了一个iOS8的定位问题,执行操作之后,不会调用到定位之后的delegate方法中,然后找了一些资料来了解了一下ios8系统下的定位,发现确实是有所不同的:

解决方法:

1.在info.plist中添加key;

NSLocationWhenInUseDescription,允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述

2.在代码定位中,做版本区分和授权请求:

  1. if ([CLLocationManager locationServicesEnabled])  
  2. {  
  3.     if (!self.locationManager)  
  4.     {  
  5.         self.locationManager = [[CLLocationManager alloc] init];  
  6.     }  
  7.     self.locationManager.delegate = self;  
  8.     self.locationManager.distanceFilter=1.0;  
  9.     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;  
  10.       
  11.     if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])  
  12.     {  
  13.         [self.locationManager requestAlwaysAuthorization]; // 永久授权  
  14.         [self.locationManager requestWhenInUseAuthorization]; //使用中授权  
  15.     }  
  16.       
  17.     [self.locationManager startUpdatingLocation];//开启位置更新  
  18.     self.delegate = delegate;  
  19. }  


ok,解决了。 这个改动也看出了苹果对隐私授权开始进行层次设计,授权不再仅仅是局限于是否的2选1. 这是一件好事!

原文地址:https://www.cnblogs.com/hanzhuzi/p/4253301.html