![](/img/trans.png)
[英]cordova getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended
[英]Get Location Updates for iOS App Even when Suspended
2014年初,Apple已將iOS 7.0更新為7.1,以允許位置更新,即使該應用程序在前台而不在后台處於活動狀態也是如此。 我們該怎么做?
我已經閱讀了一些文章,例如Apple的iOS 7.1將修復地理位置錯誤 。 但是,即使該應用被殺死/終止/暫停,蘋果也沒有提供與此相關的大量通信,也沒有提供任何有關如何獲取位置更新的示例代碼。
我已經閱讀了iOS 7.1發行說明 。 我也找不到與此相關的任何東西。
因此,即使該應用已暫停,我們如何實際獲取iOS 7和8的位置更新?
經過嘗試核心位置框架數月的試驗和錯誤之后,即使該應用被終止/暫停,我也找到了一種解決方案來獲取位置更新。 它適用於iOS 7和8。
解決方法如下:-
如果您的應用是基於位置的移動應用,當其發生重大變化時需要監視設備的位置,那么當設備距離最近的已知位置超過500米時,iOS會向您返回一些位置坐標。 是的,即使該應用被用戶或iOS本身殺死/掛起,您仍然可以獲取位置更新。
因此,即使在應用程序被終止/暫停運行時,為了使locationManager
能夠獲取位置更新,您必須使用startMonitoringSignificantLocationChanges
方法,而不能使用startUpdatingLocation
。
當iOS要將位置更新返回給應用程序時,它將幫助您重新啟動應用程序並將鍵UIApplicationLaunchOptionsLocationKey
返回給應用程序委托方法didFinishLaunchingWithOptions
。
關鍵UIApplicationLaunchOptionsLocationKey
非常重要,您必須知道如何處理它。 收到密鑰后,您必須創建一個新的locationManager實例,並且將在locationManager委托方法didUpdateLocations
上獲取位置更新。
這是示例代碼:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.shareModel = [LocationShareModel sharedModel];
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}
return YES;
}
除了didFinishLaunchingWithOptions
方法之外,當應用程序處於活動狀態時,我還創建了locationManager
實例。 以下是一些代碼示例:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];
if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(self.shareModel.anotherLocationManager)
[self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];
self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}
我寫了一篇文章,解釋了即使該應用被終止/暫停,如何獲取iOS 7和8的位置更新的詳細信息。 我還將完整的源代碼上傳到了GitHub,其中包含有關如何測試此解決方案的步驟。
請訪問以下網址以獲取更多信息:
locationManager = [[CLLocationManager alloc] init];
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
if(IS_OS_8_OR_LATER)
{
[locationManager requestWhenInUseAuthorization];
}
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
該代碼用戶位置僅針對正在運行的地面應用程序進行更新,而不針對后台運行情況進行更新
[locationManager requestWhenInUseAuthorization];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.