定位

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 4,171评论 4 19
  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 3,314评论 1 2
  • CoreLocation框架 一. iOS8.0之前的定位(✨✨✨✨✨) 前台定位导入CoreLocation框架...
    尼古拉斯赵四爷阅读 1,005评论 0 2
  • 一、定位介绍 现在很多社交、电商、团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的。的...
    LHsunshine阅读 339评论 0 0
  • 再遇见你的时候很偶然 我还在阳光下嘻嘻哈哈 说实话我受到了惊吓 双唇紧闭 我有些犹豫 我沉浸在那天的争吵 像把刀子...
    一个肤浅的人丶阅读 161评论 0 0