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

时间:2015-01-02 13:01:19

标签: ios objective-c core-location

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

我读过一些像Apple's iOS 7.1 will fix a geolocation bug这样的文章。但是,即使应用程序被终止/终止/暂停,Apple也没有提供与此相关的大量通信,也没有关于如何获取位置更新的示例代码。

我已阅读Release Notes for iOS 7.1。我也找不到与此相关的任何内容。

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

2 个答案:

答案 0 :(得分: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;
 }

除了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];
}

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

请访问以下网址以获取更多信息: -

  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

答案 1 :(得分:0)

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];

代码用户位置仅更新forground app运行但不运行后台运行

[locationManager requestWhenInUseAuthorization];