https://github.com/ChenYilong/iOS9AdaptationTips/
其他參考資料:
1.定位參考 http://www.tuicool.com/articles/v6vEri
iOS開發(fā)
http://www.cocoachina.com/ios/20150618/12200.html
iOS 9適配系列教程:后臺定位
適配iOS 9后臺定位
0.jpg
Demo:GitHub地址
【iOS9在定位的問題上,有一個壞消息一個好消息】壞消息:如果不適配iOS9昨登,就不能偷偷在后臺定位(不帶藍條售碳,見圖)!好消息:將允許出現(xiàn)這種場景:同一App中的多個location manager:一些只能在前臺定位芜果,另一些可在后臺定位查蓉,并可隨時開啟或者關閉特定location manager的后臺定位。
如果沒有請求后臺定位的權限焚虱,也是可以在后臺定位的骏融,不過會帶藍條:
untitled3.png1.jpg
如何偷偷在后臺定位:請求后臺定位權限:
// 1. 實例化定位管理器
_locationManager = [[CLLocationManager alloc] init];
// 2. 設置代理
_locationManager.delegate = self;
// 3. 定位精度
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];``
// 4.請求用戶權限:分為:?只在前臺開啟定位?在后臺也可定位链嘀,
//注意:建議只請求?和?中的一個,如果兩個權限都需要档玻,只請求?即可怀泊,
//??這樣的順序,將導致bug:第一次啟動程序后误趴,系統(tǒng)將只請求?的權限霹琼,?的權限系統(tǒng)不會請求,只會在下一次啟動應用時請求?
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
//[_locationManager requestWhenInUseAuthorization];//?只在前臺開啟定位``
[_locationManager requestAlwaysAuthorization];//?在后臺也可定位
}
// 5.iOS9新特性:將允許出現(xiàn)這種場景:同一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系統(tǒng)中卻無法定位了模孩。尖阔。。榨咐。這是一大問題敖槿础!
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数焊、實現(xiàn)CLLocationManagerDelegate的代理方法
#pragma mark CLLocationManagerDelegate<br>/**<br>* 獲取經(jīng)緯度<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前需要調用如下函數(shù)【iOS8專用】:iOS8對定位進行了一些修改昌跌,其中包括定位授權的方法仰禀,CLLocationManager增加了下面的兩個方法:(1)始終允許訪問位置信息
- (void)requestAlwaysAuthorization;
(2)使用應用程序期間允許訪問位置數(shù)據(jù)
- (void)requestWhenInUseAuthorization;
示例如下:
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=10;
if (iOSVersion>=8) {
[locationManager requestWhenInUseAuthorization];//使用程序其間允許訪問位置數(shù)據(jù)(iOS8定位需要)
}
[locationManager startUpdatingLocation];//開啟定位
2、在Info.plist文件中添加如下配置:(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
untitled4.png