【iOS9在定位的问题上,有一个坏消息一个好消息】坏消息:若是不适配iOS9,就不能偷偷在后台定位(不带蓝条,见图)!好消息:将容许出现这种场景:同一App中的多个location manager:一些只能在前台定位,另外一些可在后台定位,并可随时开启或者关闭特定location manager的后台定位。 app
若是没有请求后台定位的权限,也是能够在后台定位的,不过会带蓝条: ui
如何偷偷在后台定位:请求后台定位权限: 代理
// 1. 实例化定位管理器 源码
_locationManager = [[CLLocationManager alloc] init]; it
// 2. 设置代理 io
_locationManager.delegate = self; 后台
// 3. 定位精度 配置
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; date
// 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 配置以下:
对应的 Info.plist 的XML源码是: