【问题标题】:PhoneGap 3.x / iOs6 iOs7 / AngularJs - Plugin objects aren't available with custom www/index.html locationPhoneGap 3.x / iOs6 iOs7 / AngularJs - 插件对象不适用于自定义 www/index.html 位置
【发布时间】:2013-11-12 22:04:54
【问题描述】:

如何访问 javascript 插件对象并使用自定义 www/index.html 位置?

当我将此行添加到 project_name/www/config.xml 中时:

<content src="app/index.html" />

我可以为 www/index.html 加载自定义位置,但我无法访问任何已安装的 Cordova 插件。

下面的代码在加载默认的 www/index.html 时报告成功。

从www/app/index.html加载index.html时,navigator.connection未定义,报错:

'undefined' is not an object (evaluating 'navigator.connection.type')

这里是初始化phonegap的代码,等待设备准备好,初始化angularJS,然后通过插件检查连接。

var app = {
    initialize: function() {
        this.bindEvents();
    },

    bindEvents: function() {
            document.addEventListener("deviceready", this.onDeviceReady, false);
    },

    onDeviceReady: function() {
        angular.bootstrap(document, ["myApp"]);
        checkConnection();
    },
}
app.initialize();

function checkConnection() {
    alert('Reporting connection information');
    try{
        alert(navigator.connection.type);
    } catch(error) {
        alert('Error' + error);
        alert(navigator);
    }
}

附加信息:

  • 此行为与 PhoneGap 3.0 和 3.1 一致
  • 本机插件文件确实存在于 project_name/platforms/ios/Plugins 目录中(即:CDVConnection.m & CDVConnection.h)
  • 我正在使用 AngularJS 1.2
  • 通过 iOs6、iOs7 测试
  • XCode 5.0 和 5.2
  • 在 Phonegap 3.1 中,XCode 日志报告“2013-11-12 13:39:14.179 ESA Mobile[936:60b] 无限访问网络资源”。表明插件是在本机代码中加载的,我只是无权访问 javascript 对象。

【问题讨论】:

  • 张贴您的 index.html,其中包含 cordova_plugins.js
  • 邮寄是什么意思? cordova_plugins.js 位于 www/cordova_plugins.js
  • 我希望您从 index.html 中添加代码...所以在您的 index.html 中,您有 &lt;script src="cordova_plugins.js"&gt; 或 &lt;script src="../cordova_plugins.js"&gt;?
  • cordova_plugins.js 应该包含在 www/phonegap.js 中,并且不必特别包含,尽管这是故障排除的好方向。我用the following answer 解决了这个问题。我还没有深究其背后的原因。感谢您提供帮助。

标签: ios angularjs plugins cordova


【解决方案1】:

在文件www/app/index.html

中

不要使用:

<script src="../phonegap.js"></script>

相反,使用 cordova 文件解决了问题:

<script src="../cordova.js"></script>

注意:我用 phonegap 3.1 和 cordova 3.1 验证了这一点。我没有对 3.0 进行回归测试。

【讨论】:

  • 嗨,布赖恩,是的,我已经检查过了。实际上,我正在使用 Facebook 登录,并且在其中,我有 import cordova.js 而不是 phonegap.js。它运行良好。但是有一个问题......我也在该项目中使用社交插件。这与 Phonegap.js 一起使用。因此,当我评论 phonegap.js 并导入 cordova.js 时,社交插件无法正常工作。和 Facebook 工作。相反,facebook 不工作,社交插件工作。那么你对此有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多