定位

2015-12-15  本文已影响101人  俊月

https://github.com/ChenYilong/iOS9AdaptationTips/

其他参考资料:
1.定位参考 http://www.tuicool.com/articles/v6vEri

iOS开发

http://www.cocoachina.com/ios/20150618/12200.html

iOS 9适配系列教程:后台定位

适配iOS 9后台定位

0.jpg

Demo:GitHub地址

【iOS9在定位的问题上,有一个坏消息一个好消息】坏消息:如果不适配iOS9,就不能偷偷在后台定位(不带蓝条,见图)!好消息:将允许出现这种场景:同一App中的多个location manager:一些只能在前台定位,另一些可在后台定位,并可随时开启或者关闭特定location manager的后台定位。

如果没有请求后台定位的权限,也是可以在后台定位的,不过会带蓝条:

untitled3.png1.jpg

如何偷偷在后台定位:请求后台定位权限:

// 1. 实例化定位管理器
_locationManager = [[CLLocationManager alloc] init];
// 2. 设置代理
_locationManager.delegate = self;
// 3. 定位精度
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];``
// 4.请求用户权限:分为:?只在前台开启定位?在后台也可定位,
//注意:建议只请求?和?中的一个,如果两个权限都需要,只请求?即可,
//??这样的顺序,将导致bug:第一次启动程序后,系统将只请求?的权限,?的权限系统不会请求,只会在下一次启动应用时请求?

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
   //[_locationManager requestWhenInUseAuthorization];//?只在前台开启定位``
   [_locationManager requestAlwaysAuthorization];//?在后台也可定位
}
// 5.iOS9新特性:将允许出现这种场景:同一app中多个location manager:一些只能在前台定位,另一些可在后台定位(并可随时禁止其后台定位)。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
   _locationManager.allowsBackgroundLocationUpdates = YES;
}
// 6. 更新用户位置
[_locationManager startUpdatingLocation];
但是如果照着这种方式尝试,而没有配置Info.plist,100%你的程序会崩溃掉,并报错:

*** Assertion failure in -[CLLocationManager setAllowsBackgroundLocationUpdates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1808.1.5/Framework/CoreLocation/CLLocationManager.m:593

要将 Info.plist 配置如下:

untitled2.png

blob.png

对应的 Info.plist 的XML源码是:

untitled1.png

http://doc.okbase.net/boyuanmeng/archive/123031.htmlblob.png

在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法定位了。。。。这是一大问题啊!

1、首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.framework使用
#import <CoreLocation/CoreLocation.h> @property (nonatomic, strong) CLLocationManager *locationManager;

2、初始化CLLocationManager并开始定位

locationManager=[[CLLocationManager alloc] init];

    locationManager.delegate=self;

    locationManager.desiredAccuracy=kCLLocationAccuracyBest;

    locationManager.distanceFilter=10;

    [locationManager startUpdatingLocation];//开启定位

3、实现CLLocationManagerDelegate的代理方法

#pragma mark CLLocationManagerDelegate<br>/**<br>* 获取经纬度<br>*/

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    CLLocation *currLocation=[locations lastObject];

    location.strLatitude=[NSString stringWithFormat:@"%f",currLocation.coordinate.latitude];

    location.strLongitude=[NSString stringWithFormat:@"%f",currLocation.coordinate.longitude];

    NSLog(@"la---%f, lo---%f",currLocation.coordinate.latitude,currLocation.coordinate.longitude);

}

/**

 *定位失败,回调此方法

 */
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    if ([error code]==kCLErrorDenied) {

        NSLog(@"访问被拒绝");

    }

    if ([error code]==kCLErrorLocationUnknown) {

        NSLog(@"无法获取位置信息");

    }

}

iOS8中使用CoreLocation定位

1、在使用CoreLocation前需要调用如下函数【iOS8专用】:iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:(1)始终允许访问位置信息

- (void)requestAlwaysAuthorization;
(2)使用应用程序期间允许访问位置数据

- (void)requestWhenInUseAuthorization;

示例如下:

    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=10;
    if (iOSVersion>=8) {
        [locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)
    }
    [locationManager startUpdatingLocation];//开启定位

2、在Info.plist文件中添加如下配置:(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
untitled4.png

上一篇 下一篇

猜你喜欢

热点阅读