星空网 > 软件开发 > 操作系统

iOS iOS9.0 的CoreLocation定位

一、简介

  • iOS9.0如果当前处于前台授权状态,默认是不可以后台获取用户位置。
    • 如果在前台授权下,让其能获取到后台定位,该怎么办
      • 可以设置以下属性为YES,就可以继续获取后台位置,但是会出现蓝条
      • 使用注意:必须设置对应的后台模式,勾选后台模式:location updates
        @property(assign,nonatomic) BOOL allowsBackgroundLocationUpdates
  • 注意:iOS9.0 可以单次请求用户位置

    - (void)requestLocation // 成功调用,locations位置数组,元素按照时间排序-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations // 失败调用-(void)locationManager:(nonnull CLLocationManager *)manager didFailWithError:(nonnull NSError *)error
    • requestLocation 作用:
      • 按照定位精确度从低到高进行排序,逐个进行定位。如果获取到的位置不是精确度最高的那个,也会在定位超时后,通过代理告诉外界(必须实现代理的-locationManager:didFailWithError:方法)
      • 不能与startUpdatingLocation方法同时使用

二、实现步骤

1、 前台定位

  • 1.导入CoreLocation框架和对应的主头文件
     #import <CoreLocation/CoreLocation.h>
  • 创建CLLcationManager对象,并设置代理 请求前台定位授权,并配置KEY
    iOS iOS9.0 的CoreLocation定位images/loading.gif' data-original="http://upload-images.jianshu.io/upload_images/831339-32f859560b8e1f7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" data-original-src='/images/loading.gif' data-original="http://upload-images.jianshu.io/upload_images/831339-32f859560b8e1f7d.png?imageMogr2/auto-orient/strip%7CimageView2/2" />
    配置KEY .png

    _locationM = [[CLLocationManager alloc] init]; _locationM.delegate = self; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)    {     [_locationM requestWhenInUseAuthorization];   }
  • 3.调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
    [_locationM startUpdatingLocation];
  • 4.实现代理方法,接收位置参数
    -(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations

2、后台定位

  • 方案一:在APP处于前台定位授权场景下,勾选后台运行模式update locations (如下图) 并且,调用以下方法,设置允许后台定位

    iOS iOS9.0 的CoreLocation定位
    勾选后台模式.png

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {  _locationM.allowsBackgroundLocationUpdates = YES;}
    • 注意:如果APP处于后台,则会出现蓝条
  • 方案二:请求前后台定位授权,并配置KEY

    [_locationM requestAlwaysAuthorization];



文/iOS_成才录(简书作者)
原文链接:http://www.jianshu.com/p/a9a94cbcb775
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。



原标题:iOS iOS9.0 的CoreLocation定位

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美亚外运:https://www.goluckyvip.com/tag/38616.html
美易国际物流:https://www.goluckyvip.com/tag/38617.html
美元收款账户:https://www.goluckyvip.com/tag/38618.html
美元树 Dollar Tree :https://www.goluckyvip.com/tag/38619.html
美悦优选:https://www.goluckyvip.com/tag/38620.html
美运达跨境电商:https://www.goluckyvip.com/tag/38621.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流