我正在使用PhoneGap应用程序,并且在安装一个特定模块时出现问题,"Network Information"(https://github.com/apache/cordova-plugin-network-information) . phonegap -v
显示我正在运行5.3.7版
所有其他插件似乎工作正常 . 这是应用程序中的一个问题我也设法在一个新的应用程序中重现它:在这个例子中我唯一改变的两个文件是index.html和js / index.js,并且没有js / cordova . js文件,自动包含(Cordova Network and Camera API returns undefined)
我使用以下命令创建了应用程序:
phonegap create ios-test
cd ios-test
phonegap cordova plugin add cordova-plugin-dialogs
phonegap cordova plugin add cordova-plugin-network-information
phonegap cordova插件列表的输出是:
cordova-plugin-dialogs 1.1.1 "Notification"
cordova-plugin-network-information 1.0.1 "Network Information"
基于一个建议here我似乎有所作为 .
这是HTML(index.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
和JS:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
navigator.notification.alert('Test', null, 'Test', 'OK');
setTimeout(function() {
navigator.notification.alert('Debug', null, 'Checking connection', 'OK');
if (navigator.connection == undefined) {
navigator.notification.alert('navigator.connection is undefined', null, 'Error', 'OK');
return;
}
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
navigator.notification.alert('Network Status', null, 'Connection type: ' + states[networkState], 'OK');
}, 5000);
}
};
在我的iPhone上使用 phonegap serve
和Developer app运行代码后,我收到Debug警报,然后查看"navigator.connection is undefined" .
我也试过为iOS构建:
phonegap platform add ios
Adding ios project...
Running command: /Users/James/.cordova/lib/npm_cache/cordova-ios/3.9.2/package/bin/create /Web/ios-test/platforms/ios com.phonegap.helloworld "Hello World" --cli
iOS project created with cordova-ios@3.9.2
Discovered plugin "cordova-plugin-whitelist" in config.xml. Installing to the project
Fetching plugin "cordova-plugin-whitelist@1" via npm
Installing "cordova-plugin-whitelist" for ios
Installing "cordova-plugin-dialogs" for ios
Installing "cordova-plugin-network-information" for ios
这会创建一个 platforms/ios
文件夹,但我仍然遇到同样的问题 .
我也尝试过:
-
检查ARC已启用,iOS8 phonegap cordova network-information app crashes
-
将以下内容添加到
config.xml
Check internet connection on iOS app with Cordova Phonegap 3.3.0 not working:
<feature name =“NetworkStatus”>
<param name =“ios-package”value =“CDVConnection”/> </特征>
-
改为使用
navigator.network.connection.type
,Check internet connection on iOS app with Cordova Phonegap 3.3.0 not working -
检查AudioToolbox.framework和SystemConfiguration.framework都添加:Cordova Network Plugin does not build on iOS
2 回答
您没有正确使用
this
上下文 . 这是一个常见的错误 . Javascriptthis
不像Javathis
那样工作 .它不起作用的原因是因为
this
在 run-time 得到解决,而不是汇编时(或编译时) . 当事件触发时,this
解析为全局this
,因为您的app
对象现在已超出范围 . 该事件触发app
对象的* * .快速解决方法是做
app.onDeviceReady
而不是this.onDeviceReady你可以通过使onDeviceReady()
成为一个全局函数并保持this
来测试这个 .OHH,那个
setTimeout()
答案是那些不知道他们需要等待deviceready
事件的人 . 在Javascript世界中,糟糕的代码和糟糕的建议比比皆是 .这些视频应有所帮助 . - 最好的运气 .
Context in JavaScript - 1/4 - Purpose and Problems with JavaScript's "This"
Context in JavaScript - 2/4 - How JavaScript Decides What "This" Actually Is
Context in JavaScript - 3/4 - "This" May Not Be What You Expected & How to Fix It
Context in JavaScript - 4/4 - Mastering "This:" Additional Techniques & Future Support
对于任何感兴趣的人,我确实设法使用以下方法使用PhoneGap Build:
虽然没有运气的CLI,所以留下这个没有答案,因为这是原始的问题(并且仍然更容易使工作) .