即使在暂停时也能获取 iOS 应用的位置更新 [英] Get Location Updates for iOS App Even when Suspended

查看:45
本文介绍了即使在暂停时也能获取 iOS 应用的位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2014 年初,Apple 已将 iOS 7.0 更新至 7.1,以便即使应用在前台和后台未处于活动状态时也能进行位置更新.我们如何做到这一点?

On early 2014, Apple has updated the iOS 7.0 to 7.1 in order to allow location updates even when the app is not active on foreground and not in the background. How do we do that?

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

I have read some articles like Apple's iOS 7.1 will fix a geolocation bug. But Apple didn't provide much communication related to that nor any sample code on how to get the location update even when the app is killed/terminated/suspended.

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

I have read the Release Notes for iOS 7.1. I couldn't find anything related to that as well.

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

So, how do we actually get the location update for iOS 7 and 8 even when the app is suspended?

推荐答案

通过尝试Core Location框架,经过数月的试验和错误,我找到了即使在应用被杀死/暂停.它适用于 iOS 7 和 8.

After months of trials and errors by experimenting the Core Location Framework, I have found the solution to get location update even when the app is killed/suspended. It works well for both iOS 7 and 8.

这是解决方案:-

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

If your app is a location based mobile application that needs to monitor the location of the device when it has significant changes, the iOS will return you some location coordinates when the device has moved more than 500 meters from the last known location. Yes, even when the app is killed/suspended either by the user or iOS itself, you still can get the location updates.

所以为了让 locationManager 即使在应用程序被终止/挂起时也能获得位置更新,您必须使用方法 startMonitoringSignificantLocationChanges,您不能使用 开始更新位置.

So in order for a locationManager to get location update even when the app is killed/suspended, you must use the method startMonitoringSignificantLocationChanges, you can not use startUpdatingLocation.

当 iOS 想要将位置更新返回给应用时,它会帮助你重新启动应用并返回一个密钥 UIApplicationLaunchOptionsLocationKey 给应用委托方法 didFinishLaunchingWithOptions.

When iOS wants to return the location update to the app, it will help you to relaunch the app and return a key UIApplicationLaunchOptionsLocationKey to the app delegate method didFinishLaunchingWithOptions.

UIApplicationLaunchOptionsLocationKey这个键非常重要,你必须知道如何处理它.您必须在收到密钥时创建一个新的 locationManager 实例,您将在 locationManager 委托方法 didUpdateLocations 上获得位置更新.

The key UIApplicationLaunchOptionsLocationKey is very important and you must know how to handle it. You must create a new locationManager instance when you receive the key and you will get the location update on the locationManager delegate method 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 实例.以下是一些代码示例:

In addition to the didFinishLaunchingWithOptions method, I have created the locationManager instance when the app is active. Here are some code examples:

- (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 上上传了完整的源代码,其中包含有关如何测试此解决方案的步骤.

I have written an article explaining on the details on how to get the location update for iOS 7 and 8 even when the app is killed/suspended. I have also uploaded the complete source code on GitHub with the steps on how to test this solution.

请访问以下网址了解更多信息:-

Please visit the following URLs for more information:-

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

这篇关于即使在暂停时也能获取 iOS 应用的位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆