title: Cordova background mode geo-location iOS 10
body: <p>In my cordova project I'm using a combination of Katzer's <strong>cordova-plugin-background-mode</strong> and Mauron85's <strong>background-geolocation</strong> plugins to enable location tracking in the background. Both plugins are the latest stable version.</p>
  
  <p>This is working fine on android and ios 8, 9. But now with the release of <strong>ios 10</strong> we ran into a problem. I ran some tests and it seems that the background plugin is not consistently updating the mode (background or foreground) to the correct position. This makes our app very glitchy.</p>
  
  <p>The code we use to enable / disable background mode looks like this:</p>
  
  <pre><code>    function enableBackgroundMode() {
          if ($rootScope.ionicReady &amp;&amp; window.cordova &amp;&amp; !cordova.plugins.backgroundMode.isEnabled()) {
              cordova.plugins.backgroundMode.enable();
          }
      }
  
      function disableBackgroundMode() {
          if ($rootScope.ionicReady &amp;&amp; window.cordova &amp;&amp; cordova.plugins.backgroundMode.isEnabled()) {
              cordova.plugins.backgroundMode.disable();
          }
      }
  
      function isRunningInBackground() {
          return $rootScope.ionicReady &amp;&amp; window.cordova &amp;&amp; cordova.plugins.backgroundMode.isActive();
      }
  </code></pre>
  
  <p>When isRunningInBackground() is returning true, we call the following function to retrieve the user his location in the background.</p>
  
  <pre><code>function getCurrentLocationInBackground(interval) {
    var deferred = $q.defer();
  
    backgroundGeolocation.configure((location) =&gt; {
      backgroundGeolocation.finish();
      LogService.debug("Latitude: " + location.latitude + ", Longitude: " + location.longitude);
      deferred.resolve(location);
    }, (error) =&gt; {
      LogService.error('BackgroundGeoLocation error');
      deferred.reject(error);
    }, {
      desiredAccuracy: 10, //10m accurate
      stationaryRadius: 5, // if stationary, device must move beyond 5m to engage background-tracking
      distanceFilter: 5, //min distance a device must move before update event is generated
      stopOnTerminate: true, // &lt;-- enable this to clear background location settings when the app terminates
      notificationTitle: translations['generic.background.tracking.title'],
      notificationText: translations['generic.background.tracking.text'],
      locationProvider: backgroundGeolocation.provider.ANDROID_ACTIVITY_PROVIDER,
      activityType: "AutomotiveNavigation",
      interval: interval ? parseInt(interval) : 30000
    });
  
    if (window.cordova) {
      backgroundGeolocation.start();
    }
  
  
    return deferred.promise;
  }
  </code></pre>
  
  <p>However, on ios 10 if we put our app on the background, and return again to the foreground there is a big chance that cordova.plugins.backgroundMode.isActive() still is returning true which triggers our background logic.. </p>
  
  <p>I've been debugging this for a while now, but I can't see the problem.</p>