我正在努力在iPhone上安装PhoneGap插件。我试图安装的插件的页面可以在这里看到:https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/MessageBox。
我相信我已经将我的问题缩小到错误地使用JavaScript文件。我将它包含在我的HTML页面上,如下所示:
<script language="javascript" type="text/javascript" src="MessageBox.js"></script>
我的HTML页面的其余部分是:
<script type="text/javascript">
alert("test1");
var messageBox = window.plugins.messageBox;
alert("test2");
messageBox.alert('Title', 'Message', function(button) { console.warn('alert', [this, arguments]); });
</script>
我看到一个警告说 test1 ,但不是第二个警报。这让我觉得错误就行了:
var messageBox = window.plugins.messageBox;
但是,我不太确定我应该采取哪些不同的做法。据我所知,我已经完成了插件文档页面中描述的所有必要步骤,如下所示: https://github.com/phonegap/phonegap-plugins/blob/master/iPhone/MessageBox/README.md
(正如预期的那样,当通过iOS模拟器查看时,我也看不到messageBox.alert ...行的输出。)
感谢您对此问题的任何帮助,谢谢!
注意:我可以在此处看到有关此主题的初始主题:Trouble Installing PhoneGap Plugin
编辑:我还应该补充一点,我在尝试安装一个不同(但相似)的插件时遇到了完全相同的问题,称为“提示”
EDIT2:这是我的index.html:
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" src="MessageBox.js"></script>
<script type="text/javascript">
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady()
{
navigator.notification.alert("PhoneGap is working");
window.location.href="otherpage.html";
}
</script>
答案 0 :(得分:1)
问题在于,当您尝试创建MessageBox时,PhoneGap还没有准备好。
在执行代码之前,您只需要等待PhoneGap准备就绪:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.location="otherpage.html";
}
</script>
</head>
<body>
</body>
</html>
然后,在otherpage.html上:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" charset="utf-8" src="MessageBox.js"></script>
</head>
<body onload="onLoad()">
<script type="text/javascript" charset="utf-8">
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
console.log("onLoad");
var messageBox = window.plugins.messageBox;
messageBox.alert('Title', 'Message', function(button) { console.warn('alert', [this, arguments]); });
}
</script>
</body>
</html>
我在PhoneGap 1.4.1
上测试了它