获取iOS应用程序的位置更新即使在暂停状态

在2014年初,苹果公司已经将iOS 7.0更新到7.1,以便即使应用程序在前台而不是在后台处于活动状态时也允许进行位置更新。 我们怎么做?

我已经阅读了一些文章,如苹果的iOS 7.1将修复一个地理定位错误 。 但是苹果公​​司并没有提供相关的通讯,也没有提供任何关于如何获取位置更新的示例代码,即使应用程序被终止/终止/挂起。

我已阅读iOS 7.1发行说明 。 我也找不到与此相关的任何内容。

那么,即使应用程序被暂停,我们如何才能真正获得iOS 7和8的位置更新?

经过几个月的试验和错误的实验核心位置框架,我已经find了解决scheme来获取位置更新,即使应用程序被杀/暂停。 它适用于iOS 7和8。

这是解决办法:

如果您的应用程序是基于位置的移动应用程序,当设备发生重大变化时需要监视该设备的位置,则当设备从最后一个已知位置移动超过500米时,iOS会返回一些位置坐标。 是的,即使应用程序被用户或iOS本身杀死/暂停,您仍然可以获取位置更新。

因此,即使应用程序被终止/挂起,为了使locationManager获取位置更新,您必须使用startMonitoringSignificantLocationChanges方法,但不能使用startUpdatingLocation

当iOS想要将位置更新返回给应用程序时,它将帮助您重新启动应用程序,并将UIApplicationLaunchOptionsLocationKey键返回给应用程序委托方法didFinishLaunchingWithOptions

关键UIApplicationLaunchOptionsLocationKey是非常重要的,你必须知道如何处理它。 您必须在收到密钥时创build一个新的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方法之外,我还在应用程序处于活动状态时创build了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上的完整源代码以及如何testing这个解决scheme的步骤。

请访问以下url获取更多信息:

  1. 当应用程序被终止/终止时,获取iOS 7和8的位置更新
  2. GitHub的源代码 – 获取位置更新即使iOS移动应用程序被暂停/终止/终止
 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];