获取iOS App的位置更新即使暂停

Ric*_*cky 31 objective-c core-location ios

2014年初,Apple已将iOS 7.0更新为7.1,以便即使应用程序在前台而非后台处于活动状态时也允许位置更新.我们怎么做?

我读过一些文章,比如Apple的iOS 7.1将修复地理定位错误.但是,即使应用程序被终止/终止/暂停,Apple也没有提供与此相关的任何通信,也没有提供有关如何获取位置更新的示例代码.

我已阅读iOS 7.1发行说明.我也找不到任何相关的东西.

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

Ric*_*cky 76

通过试验核心位置框架经过数月的试验和错误后,即使应用程序被杀死/暂停,我也找到了获取位置更新的解决方案.它适用于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;
 }
Run Code Online (Sandbox Code Playgroud)

除了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];
}
Run Code Online (Sandbox Code Playgroud)

我写了一篇文章解释有关如何获取iOS 7和8的位置更新的详细信息,即使应用程序被杀死/暂停也是如此.我还在GitHub上传了完整的源代码,其中包含如何测试此解决方案的步骤.

有关更多信息,请访问以下网址: -

  1. 应用程序被终止/终止/暂停时获取iOS 7和8的位置更新
  2. GitHub上的源代码 - 获取位置更新即使iOS移动应用程序被暂停/终止/终止

  • 好问题.当应用程序挂起时,您之前创建的共享位置管理器也将从内存中刷新.如何重用内存中不存在的实例?因此,创建新实例是唯一的方法. (7认同)
  • 当应用程序终止或仅在.plist文件中时,是否可以使用核心数据保存位置和日志? (2认同)