背景服务cordova离子应用程序。 Backgroudn插件不工作在ios 8.3 [英] Background service cordova ionic app. Backgroudn plugin not working on ios 8.3

查看:621
本文介绍了背景服务cordova离子应用程序。 Backgroudn插件不工作在ios 8.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个将地理位置发送到服务器的后台服务。因此,我使用了来自


< hr>

StartUpdatingLocations是一种位置跟踪启动方法,它将导致每秒调用位置更改的回调,除非你的应用程序在前台/后台。 strong>



无论位置是否改变,每秒都会调用回调,这取决于处理和决定位置更改的方法,实际上它是一个位置更改。



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



StartUpdatingLocations提供最准确的位置更新












  1. StartMonitoringSignificantLocationChanges是一种位置跟踪启动方法,只要用户位置有重大更改,就会调用位置更改回调。


  2. 此方法主动


  3. StartMonitoringSignificantLocationChanges可以继续更新位置,即使应用程序处于后台/终止时/暂停。


  4. 此方法的位置更新不是很可靠,个人使用情况表示位置更新有点随意,并且严重依赖于蜂窝位置更新。







现在您要做的是容易在Android,但它不是在iOS。



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



iOS 7和8中的位置连续使用方法startUpdatingLocation

  [myLocationManager startUpdatingLocation]; 

然后下一个招数就是委托方法didUpdateLocations。您将必须使用一个计时器并适当地处理后台任务。



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

  [anotherLocationManager startMonitoringSignificantLocationChanges]; 

另一个重要的技巧是,你必须知道如何处理应用程序中的UIApplicationLaunchOptionsLocationKey委托didFinishLaunchingWithOptions。这里是示例代码: -

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


I want to implement a background service which sends geolocations to a server. Therefore I used the plugin cordova-plugin-background-mode from https://github.com/katzer/cordova-plugin-background-mode, which works with android.

But if I run the app on iOS 8.3 and push the home button the app stops sending the geolocations to the server. In the documentation of the plugin it says:

Supported Platforms

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

Am I missing something?

Edit: Here is some code from my controller

$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;
    });
});

解决方案

While i was exploring the same stuff, there's essentially no pure Hybrid way to achieve background location tracking. But, if you're interested, you can implement the same using native iOS implementation.

Before you go ahead with Apple Background location tracking, you must get a few details upon the possible approaches.

An apple app can track location in two way, and they are as follows :



StartUpdatingLocations is a location tracking initiation method that will cause the callback for location change being invoked every second until and unless you’re app is in foreground/background.

The callback will be called every second regardless of the fact whether the location has changed or not, it is up to the method to handle and decide the location change, and that in fact it is a location change.

StartUpdatingLocations will have no effect when the app is in suspended/terminated. This essentially means that even if StartUpdatingLocations has been called when the app started, as soon as the app moves in background, the callback will no longer be invoked, even if there’s any location change.

StartUpdatingLocations provides most accurate location updates.



  1. StartMonitoringSignificantLocationChanges is a location tracking initiation method that will cause the callback for location change being invoked whenever there’s a SIGNIFICANT CHANGE IN THE USER LOCATION.

  2. This method pro-actively uses Cellular tower location changes, and as documented provides location updates every 500-1000 meters.

  3. StartMonitoringSignificantLocationChanges can continue updating locations even when the app is in background/terminated/suspended.

  4. Location updates from this method ain’t very reliable and personal usage indicates that the location updates are a bit haphazard and are heavily dependent on cellular location updates.


Now what you're trying to do is quite easy in Android, but it ain't in iOS.

I would not reinvent the cycle, but you can explore the complete details here

The method to get the location in the background continuously for iOS 7 and 8 is using the method "startUpdatingLocation"

[myLocationManager startUpdatingLocation];

and then the next trick would be on the delegate method "didUpdateLocations". You will have to use a timer and handle the Background Task appropriately. Any missing steps and the location will not be updated continuously.

But in the case of getting the location when the app is terminated/suspended, You can not use [myLocationManager startUpdatingLocation]; The only way to make it work is to use:-

[anotherLocationManager startMonitoringSignificantLocationChanges];

Another important trick is, you will have to know how to handle the key "UIApplicationLaunchOptionsLocationKey" on the app delegate "didFinishLaunchingWithOptions". 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;
    }

In addition to the didFinishLaunchingWithOptions method, You have to create a 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];
    }

这篇关于背景服务cordova离子应用程序。 Backgroudn插件不工作在ios 8.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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