背景服务科多瓦离子应用程序。 Backgroudn插件不工作在ios 8.3上

我想要实现一个后台服务,将地理位置发送到服务器。 因此,我使用https://github.com/katzer/cordova-plugin-background-mode的插件cordova-plugin-background-mode,它与android一起工作。

但是,如果我在iOS 8.3上运行应用程序并按下主页button,应用程序将停止将地理位置发送到服务器。 在插件的文档中,它说:

支持的平台

  1. iOS(包括iOS8)
  2. Android(SDK> = 11)
  3. WP8

我错过了什么吗?

编辑:这是我的控制器的一些代码

$ionicPlatform.ready(function() { var watchOptions = { frequency : 1000, timeout : 5*60*1000, enableHighAccuracy: true }; var watch = $cordovaGeolocation.watchPosition(watchOptions); watch.then( null, function(err) { alert("WatchPosition failed: "+JSON.stringify(err)); }, function(position) { $scope.position = position; }); }); 

当我正在探索相同的东西,基本上没有纯粹的混合方式来实现背景位置跟踪。 但是,如果您有兴趣,可以使用本机iOS实现来实现相同的function。

在开始Apple背景位置跟踪之前,您必须详细了解可能的方法。

一个苹果应用程序可以用两种方式跟踪位置,它们如下所示:

在这里输入图像说明


  • startUpdatingLocations

StartUpdatingLocations是一个位置跟踪启动方法,会导致每秒调用一次位置更改的callback,除非您的应用程序位于前台/后台。

无论位置是否发生变化,每秒钟都会调用callback函数,取决于处理和决定位置变化的方法,实际上是位置变化。

当应用处于暂停/终止状态时,StartUpdatingLocations将不起作用。 这基本上意味着,即使StartUpdatingLocations在应用程序启动时被调用,只要应用程序在后台移动,即使有位置更改,也不会再调用callback。

StartUpdatingLocations提供最准确的位置更新。


  • startMonitoringSignificantLocationChanges

  1. StartMonitoringSignificantLocationChanges是一个位置跟踪启动方法,当用户位置发生重大改变时,将引起位置改变的callback被调用。

  2. 这种方法主动使用蜂窝塔位置变化,并且按照logging提供每500-1000米的位置更新。

  3. 即使应用程序处于后台/终止/挂起状态,StartMonitoringSignificantLocationChanges也可以继续更新位置。

  4. 从这个方法的位置更新不是很可靠,个人使用表明,位置更新有点杂乱,并严重依赖于蜂窝位置更新。


现在你想要做的事情在Android中很容易,但它不在iOS中。

我不会重复这个循环,但是你可以在这里探索完整的细节

iOS 7和8中连续获取位置的方法是使用“startUpdatingLocation”

 [myLocationManager startUpdatingLocation]; 

然后下一个技巧将在委托方法“didUpdateLocations”。 您将不得不使用计时器并适当地处理后台任务。 任何遗漏的步骤和位置将不会持续更新。

但是在获取应用程序终止/暂停的位置的情况下,您不能使用[myLocationManager startUpdatingLocation]; 使其工作的唯一方法是使用: –

 [anotherLocationManager startMonitoringSignificantLocationChanges]; 

另一个重要的技巧是,您将不得不知道如何处理应用程序委托“didFinishLaunchingWithOptions”上的关键“UIApplicationLaunchOptionsLocationKey”。 以下是示例代码:

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

我发现我必须在xcode的资源下的projectname-Info.plist中添加背景模式,如下所示:

的Info.plist

现在,即使应用程序在后台或设备被locking,它也会发送地理位置信息。