定位

作者: 俊月 | 来源:发表于2015-12-15 09:04 被阅读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

相关文章

  • 定位,定位,还是定位

    要想成为一个好的文字的输出者,首先要给自己一个合理的定位。定位可以首先想象自己想成为的角色:一个能够通过各种故事启...

  • 10-CSS定位流

    定位流 定位流分类相对定位绝对定位固定定位静态定位 相对定位 什么是相对定位?相对定位就是相对于自己以前在标准流中...

  • CSS中的几种定位

    CSS中常用的定位有 普通定位,相对定位 绝对定位、fixed定位 浮动 1、普通定位和相对定位 css中的元素有...

  • 2017-02-23 CSS 学习笔记

    定位流 定位流分类 相对定位 绝对定位 固定定位 静态定位 什么是相对定位相对定位就是相对于自己以前在标准中的位置...

  • #30天专注橙长计划#品牌定位30(6)营销推广1

    品牌定位的维度包括: 市场定位、价格定位、形象定位、地理定位、人群定位、渠道定位等; 经过前期大量的市场调研工作,...

  • D28:系统地图, 高德地图

    目录一. 定位功能 iPhone手机定位(基站定位, WiFi定位, GPS定位: 定位所需时间递增, 精确度递增...

  • 相对定位、绝对定位和固定定位

    1. 相对定位 定位有三种:相对定位,绝对定位、固定定位。 相对定位: position:relative lef...

  • 定位流

    定位流指的是:相对定位(relative)/绝对定位(absolute)/固定定位(fixed) 不包括静态定位(...

  • CSS补充2

    定位position 定位分为四种:相对定位 relative,绝对定位 absolute,固定定位 fixed,...

  • 定位

    1、static定位(普通流定位) -------------- 默认定位 2、float定位(浮动定位) 例:f...

网友评论

      本文标题:定位

      本文链接:https://www.haomeiwen.com/subject/ckuuhttx.html