2 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
通過試驗(yàn)核心位置框架經(jīng)過數(shù)月的試驗(yàn)和錯(cuò)誤后,即使應(yīng)用程序被殺死/暫停,我也找到了獲取位置更新的解決方案。它適用于iOS 7和8。
這是解決方案: -
如果您的應(yīng)用是基于位置的移動應(yīng)用,需要在設(shè)備發(fā)生重大變化時(shí)監(jiān)控設(shè)備的位置,當(dāng)設(shè)備距離上一個(gè)已知位置移動超過500米時(shí),iOS會返回一些位置坐標(biāo)。是的,即使應(yīng)用程序被用戶或iOS本身殺死/暫停,您仍然可以獲得位置更新。
因此,locationManager
即使應(yīng)用程序被殺死/暫停,為了獲得位置更新,您必須使用該方法startMonitoringSignificantLocationChanges
,您不能使用startUpdatingLocation
。
當(dāng)iOS想要將位置更新返回給應(yīng)用程序時(shí),它將幫助您重新啟動應(yīng)用程序并將密鑰返回UIApplicationLaunchOptionsLocationKey
到應(yīng)用程序委托方法didFinishLaunchingWithOptions
。
關(guān)鍵UIApplicationLaunchOptionsLocationKey
是非常重要的,你必須知道如何處理它。您必須在收到密鑰時(shí)創(chuàng)建新的locationManager實(shí)例,并且您將獲得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
應(yīng)用程序處于活動狀態(tài)時(shí)創(chuàng)建了實(shí)例。以下是一些代碼示例:
- (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];}
我寫了一篇文章解釋有關(guān)如何獲取iOS 7和8的位置更新的詳細(xì)信息,即使應(yīng)用程序被殺死/暫停也是如此。我還在GitHub上傳了完整的源代碼,其中包含如何測試此解決方案的步驟。

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
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 movelocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;[locationManager startUpdatingLocation];
該代碼用戶位置僅更新forground app運(yùn)行但不運(yùn)行后臺運(yùn)行
[locationManager requestWhenInUseAuthorization];
- 2 回答
- 0 關(guān)注
- 832 瀏覽
添加回答
舉報(bào)