iOS9 定位遇到的一點問題

 iOS9的iPad上定位成功一次。 然後定位失敗 。   

然後代碼發給別人用別人iOS9的真手機測試,定位沒有發現任何問題。          不知道是哪出了問題。



轉載下定位適配    https://github.com/ChenYilong/iOS9AdaptationTips#1-demo1_ios9網絡適配_ats改用更安全的https

demo2_iOS9新特性_更靈活的後臺定位

【iOS9在定位的問題上,有一個壞消息一個好消息】壞消息:如果不適配iOS9,就不能偷偷在後臺定位(不帶藍條,見圖)!好消息:將允許出現這種場景:同一App中的多個location manager:一些只能在前臺定位,另一些可在後臺定位,並可隨時開啓或者關閉特定location manager的後臺定位。

如果沒有請求後臺定位的權限,也是可以在後臺定位的,不過會帶藍條:enter image description here

如何偷偷在後臺定位:請求後臺定位權限:

 // 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 配置如下:enter image description here

對應的 Info.plist 的XML源碼是:

<key>NSLocationAlwaysUsageDescription</key>
<string>微博@iOS程序犭袁 請求後臺定位權限</string>

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

第二種:

在對應 target 的 Capabilities -> Background Modes -> 開啓 Location Updates 

enter image description here






 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章