获取iOS App的位置更新即使暂停 [英] Get Location Updates for iOS App Even when Suspended

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

问题描述

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将修复地理定位错误。但是,即使应用程序被终止/终止/暂停,Apple也没有提供与此相关的任何通信,也没有提供有关如何获取位置更新的示例代码。

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的位置更新? / p>

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

推荐答案

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

以下是解决方案: -

Here is the solution:-

如果您的应用是基于位置的移动应用程序需要在设备发生重大变化时监控设备的位置,当设备从最后一个已知位置移动超过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 ,你不能使用 startUpdatingLocation

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.

下面是示例代码: -

Here is the sample code:-

- (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上也可获取位置更新移动应用已被暂停/终止/终止

  1. Getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended
  2. Source Code on GitHub - Get the Location Updates Even when the iOS mobile apps is Suspended/Terminated/Killed

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

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