一、简介

  • 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

    配置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 (如下图) 并且,调用以下方法,设置允许后台定位

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

    [_locationM requestAlwaysAuthorization];
转载:原文链接:http://www.jianshu.com/p/a9a94cbcb775

iOS iOS9.0 的CoreLocation定位的更多相关文章

  1. iOS开发拓展篇—CoreLocation定位服务

    iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...

  2. CoreLocation 定位

    前言: 本章会使用OC和Swift分别进行实现,需要了解Swift的小伙伴可以翻一下之前的博文 LBS和SoloMo(索罗门) LBS:基于位置的服务,根据定位展示周边美食.景点等信息(全称:Loca ...

  3. iOS开发——高级篇——地理定位 CoreLocation

    一.CoreLocation 在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用 ...

  4. iOS - CoreLocation 定位

    前言 NS_CLASS_AVAILABLE(10_6, 2_0) @interface CLLocationManager : NSObject 1.CoreLocation 定位 配置 1.在 iO ...

  5. [iOS 利用MapKit和CoreLocation框架打造精简的定位和导航]

    运行效果:            一.利用<CoreLocation/CoreLocation.h>定位 创建变量 CLLocationManager *locationManager , ...

  6. ios开发之 -- stringByAddingPercentEscapesUsingEncoding方法被替换 iOS9.0

    最近在项目中,发现之前的一个方法已经不被建议使用了. 该方法名即题目中提到的: stringByAddingPercentEscapesUsingEncoding,这个方法是用来进行转码的,即将汉字转 ...

  7. IOS 弹框AlterView的使用(IOS8.0以前使用)UIAlertController(IOS9.0使用)

    #pragma mark - 代理方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath ...

  8. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  9. iOS中的地图和定位

    文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location  如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ...

随机推荐

  1. [Tool] 配置文件之Web.config

    开发人员工具: 安装完vs后,(如2013:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts\VS ...

  2. DataTable转换为List<T>或者DataRow转换为T

    这段时间开发ASP.NETMVC应用程序,从数据库获取数据之后,需要把记录转换为数据集在视图中显示.我们需要把DataTable转换为List<T>或者DataRow转换为T. 本篇中可以 ...

  3. android app自动化测试之UIAutomator

    一.UIAutomator Android自动化测试工具有很多,但是要免费.易上手,本人觉得就直接使用Eclipse自带的UIAutomator就不错.测试人员无需跟开发要代码信息,只要手机上有安装之 ...

  4. Hello, Android 快速入门

    Hello, Android Android 开发与 Xamarin 简介 在这两节指南中,我们将 (使用 Xamarin Studio或 Visual Studio)建立我们的第一个 Xamarin ...

  5. JAVA获取CLASSPATH路径

    ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL getResource (String name); public InputStream getResou ...

  6. Netbeans 8.0.2 For PHP继续坑

    仍然没有修复PHP好用的代码快速导航功能,坑!

  7. poj-1703-Find them, Catch them

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41928   Accepted: ...

  8. php中的字符串常用函数(五) explode 妙用

    // 示例 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh" ; list( $user , $pass , $uid , $gid , ...

  9. mysql autocommit=OFF导致wordpress 建立数据库连接时出错

    今天安装wordpress完成后跳转到login页面时,出现建立数据库连接时出错.网上清一色的拷贝http://mt.sohu.com/20160917/n468547634.shtml的答案. 只能 ...

  10. Spring MVC处理异常的4种方式

    http://blog.csdn.net/ufo2910628/article/details/40399539 http://my.oschina.net/CandyDesire/blog/3333 ...