首页 文章

Cordova Background Fetcher插件不适用于iOS

提问于
浏览
3

我想实现这个插件:https://github.com/christocracy/cordova-plugin-background-fetch . 我使用CLI安装了插件,然后将BackgroundFetch.js文件复制到我的www目录并在index.html中引用它 . 我将示例代码放在DeviceReady函数中的git url中 . 仍然,在启动时我遇到了错误:

TypeError: 'undefined' is not an object (evaluating 'Fetcher.configure')

有人可以告诉我为什么会这样吗?我已经尝试了两天,但无济于事 . 任何帮助将不胜感激 . 提前致谢!


EDIT1: 对不起我之前没有提到它,我使用命令行创建了一个cordova项目并添加了如上所述的插件,然后在iOS模拟器和iPhone 4S上运行它 . 两者都给出了相同的错误 . 我将代码包装在try-catch块中并提醒错误,这就是错误 .

看到Clawish 's suggestion, I removed the js file and the reference to it, and removed and added the plugin again. Now I don' t得到任何错误后,我在日志中看不到"CDVBackgroundFetch configure" . 如果我模拟模拟器上的背景提取,我会收到错误: -

CDVBackgroundFetch onFetch
警告:应用程序委托收到对-application的调用:performFetchWithCompletionHandler:但从未调用完成处理程序 .

我哪里错了?除了添加示例代码以调用onDeviceReady中的Fetcher.configure之外,我没有对代码进行任何更改


EDIT2:

在看到您的Clawfish的第二次编辑后,我删除了服务调用并缩短了以下功能:

onDeviceReady: function() {
app.receivedEvent('deviceready');

var Fetcher = window.plugins.backgroundFetch;

// Your background-fetch handler.
var fetchCallback = function() {
    console.log('BackgroundFetch initiated');
    Fetcher.finish();
}
Fetcher.configure(fetchCallback);
},

仍然,我没有看到任何日志消息说“CDVBackgroundFetch configure”和模拟后台获取相同的警告如上所述 . 似乎Fetch部分正在执行而没有首先运行configure .

1 回答

  • 1

    Edit2: 我猜你已经从文档添加了整个示例代码(使用 var Fetcher 和处理程序 var fetchCallback 的decalaration,并且在声明两者之后,运行 Fetcher.configure(fetchCallback); . 问题可能是你没有在_2515528中引用jQuery并且代码停止当它看到 $.get(...) 行时运行 . 如果是这样的话,download jQuery,把它放到你的 www 文件夹中,并从你的 index.html 引用它 . 你看到控制台消息 console.log('BackgroundFetch initiated');/heartbeat.json 怎么样,你改变了吗?


    Edit: 您无需将BackgroundFetch.js复制到 www 目录,也无需在 index.html 中引用它 . 尝试删除 index.html 中的引用,并在安装后保持插件文件不变 . 如果你已经改变了关于文件结构的东西,你应该 cordova plugin remove 插件,然后再次 add .


    我不知道你从哪个控制台收到此错误 . 如果它是一个浏览器控制台,如Chrome,那么没有奇迹,因为Fetcher只能在iDevice(或iOS模拟器)上工作 . Cordova Console插件不会打印此类消息 .

    尝试使用iOS模拟器中的代码 .

相关问题