在我的 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/