iOS上的PhoneGap网络信息插件问题 [英] Issue with PhoneGap Network Information Plugin on iOS
问题描述
我正在使用PhoneGap应用程式,并且在安装一个特定的模组「网路资讯」( https://github.com/apache/cordova-plugin-network-information )。 phonegap -v
显示我正在运行版本5.3.7
I'm working on a PhoneGap app, and have an issue with installing one particular module, "Network Information" (https://github.com/apache/cordova-plugin-network-information). phonegap -v
shows I'm running version 5.3.7
所有其他插件似乎工作正常。这是我正在开发的应用程序中的一个问题,但我也设法在一个新的应用程序中重现它:在这个例子中,我改变的只有两个文件是index.html和js / index.js,是没有js / cordova.js文件,这是自动包括( Cordova网络和相机API返回未定义)
All other plugins seem to be working fine. This is an issue in the application I'm working on but I've also managed to reproduce it in a new application: The only two files I've changed are index.html and js/index.js in this example, and there is no js/cordova.js file, which is automatically included (Cordova Network and Camera API returns undefined)
我使用以下命令创建了应用程序:
I've created the app using the following commands:
phonegap create ios-test
cd ios-test
phonegap cordova plugin add cordova-plugin-dialogs
phonegap cordova plugin add cordova-plugin-network-information
phonegap cordova插件列表的输出是:
The output of phonegap cordova plugin list is:
cordova-plugin-dialogs 1.1.1 "Notification"
cordova-plugin-network-information 1.0.1 "Network Information"
根据建议此处
这里是HTML(index.html):
Here is the 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);
}
};
使用 phonegap serve
在我的iPhone上的开发者应用程序,我得到调试警报,然后看到navigator.connection是未定义的。
Upon running the code with phonegap serve
and the Developer app on my iPhone, I get the Debug alert then see "navigator.connection is undefined".
ve也尝试为iOS构建:
I've also tried to build for 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
$ c> platforms / ios 文件夹,但我仍然有同样的问题。
This creates a platforms/ios
folder, but I still have the same issue.
've also tried:
I've also tried:
-
检查ARC已启用, iOS8 phonegap cordova网络信息应用崩溃
添加以下内容到 config.xml
使用Cordova Phonegap 3.3.0在iOS应用上检查互联网连接:
- 使用
navigator.network.connection.type
而不是在iOS应用程序上检查互联网连接Cordova Phonegap 3.3.0无法正常工作 - 检查AudioToolbox.framework和SystemConfiguration.framework都添加: Cordova网络插件不建立在iOS
推荐答案
您不是使用 code>上下文。这是一个常见的错误。 Javascript
这不像Java
这个
工作。
The reason it does not work is because the this
gets resolved at run-time, not assemble-time (or compile-time). When the event fires, this
resolves to the the global this
because your app
object is now out of scope. The event fires *outside* of your app
object.
它不工作的原因是因为而不是集合时解决了这
(或编译时)。当事件触发时,此
解析为全局此
,因为您的 app
对象现在超出范围。事件触发您的应用程式
物件之外的*外部。
A quick fix would be to do app.onDeviceReady
instead of You can test this by making yourthis.onDeviceReady
onDeviceReady()
a global function and leaving the this
in place.
快速解决方法是执行 app.onDeviceReady
而不是 onDeviceReady()一个全局函数,并保留 this.onDeviceReady
this
。
OHH, and that setTimeout()
answer is someone that does not know they need to wait for the deviceready
event. Bad code and bad advice abound in the Javascript world.
这些视频应该有所帮助。 – Best of Luck。
These videos should help. – Best of Luck.
JavaScript中的上下文 - 2/4 - JavaScript如何确定这个实际上是什么
JavaScript中的上下文 - 3/4 - 此可能不是你期待&如何解决
JavaScript中的上下文 - 4/4 - 掌握这:其他技术&未来支持
这篇关于iOS上的PhoneGap网络信息插件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!