ios - Cordova后台模式地理位置iOS 10

标签 ios cordova ios10

在我的 cordova 项目中,我结合使用了 Katzer 的 cordova-plugin-background-mode 和 Mauron85 的 background-geolocation 插件,以在后台启用位置跟踪。这两个插件都是最新的稳定版本。

这在 android 和 ios 8、9 上运行良好。但是现在随着 ios 10 的发布,我们遇到了问题。我进行了一些测试,似乎背景插件没有始终如一地将模式(背景或前景)更新到正确的位置。这使我们的应用程序出现故障。

我们用来启用/禁用后台模式的代码如下所示:

    function enableBackgroundMode() {
        if ($rootScope.ionicReady && window.cordova && !cordova.plugins.backgroundMode.isEnabled()) {
            cordova.plugins.backgroundMode.enable();
        }
    }

    function disableBackgroundMode() {
        if ($rootScope.ionicReady && window.cordova && cordova.plugins.backgroundMode.isEnabled()) {
            cordova.plugins.backgroundMode.disable();
        }
    }

    function isRunningInBackground() {
        return $rootScope.ionicReady && window.cordova && cordova.plugins.backgroundMode.isActive();
    }

当 isRunningInBackground() 返回 true 时,我们调用以下函数来检索用户在后台的位置。

function getCurrentLocationInBackground(interval) {
  var deferred = $q.defer();

  backgroundGeolocation.configure((location) => {
    backgroundGeolocation.finish();
    LogService.debug("Latitude: " + location.latitude + ", Longitude: " + location.longitude);
    deferred.resolve(location);
  }, (error) => {
    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, // <-- 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;
}

然而,在 ios 10 上,如果我们将我们的应用程序置于后台,然后再次返回到前台,则很有可能 cordova.plugins.backgroundMode.isActive() 仍然返回 true,这会触发我们的后台逻辑。

我已经调试了一段时间了,但我看不出问题所在。

最佳答案

这对我来说适用于 ios10。

将此添加到您的 info.plist:

<key>NSMotionUsageDescription</key> <string>The app uses accelerometer to increase battery efficiency by intelligently toggling location-tracking</string>

关于ios - Cordova后台模式地理位置iOS 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40177299/

相关文章:

ios - 如何使用 Xcode 7.3 为 iOS 10 设备构建?

ios - 有一个非可选的弱引用有意义吗?

ios - Cordova iOS : Add method call in AppDelegate. m

iOS 10 : Receive Remote Notification in background

java - Android 杀死 PhoneGap 应用程序

cordova - ionic 构建android的错误: copyFileSync: could not write to dest file

Swift 2 到 3 迁移 String.fromCString

ios - 仅在 didSelectRowAtIndexPath 中长按

iphone - 在 iPhone 中的 ScrollView 中交换图像

ios - 使用未解析的标识符 'image'