The problem with the plugin for information about the PhoneGap network on iOS

I am working on a PhoneGap application and have a problem installing one specific Network Information module ( https://github.com/apache/cordova-plugin-network-information ). phonegap -v indicates that version 5.3.7 is running

All other plugins work fine. This is a problem in the application I'm working on, but I also managed to play it in the new application: only the two files that I changed are index.html and js / index.js in this example, and there is no js / cordova file there .js that automatically turns on ( Cordova Corporation's API and API returns undefined )

I created the application 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 

Listing phonegap cordova plugins:

 cordova-plugin-dialogs 1.1.1 "Notification" cordova-plugin-network-information 1.0.1 "Network Information" 

Based on the suggestion here, I wrapped this in a call to setTimeout (), but that does not seem to matter.

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> 

And 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); } }; 

After running the code using phonegap serve and the Developer application on my iPhone, I get a "Debug" error message and then "navigator.connection is undefined".


I also tried to create 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 

This creates the platforms/ios folder, but I still have the same problem.


I also tried:

 <feature name="NetworkStatus"> <param name="ios-package" value="CDVConnection" /> </feature> 
+5
source share
2 answers

You are not using the this context correctly. This is a common mistake. Javascript this does NOT work like Java this .

The reason this doesn't work is because this resolved at runtime rather than build time (or compile time). When the event fires, this resolves the global this , because your app object is now out of scope. The event fires * outside * your app object.

A quick fix would be to execute app.onDeviceReady instead of this.onDeviceReady . You can verify this by making your onDeviceReady() global function and leaving this in place.

OHH, and that the setTimeout() response is someone who does not know that he needs to wait for the deviceready event. Bad code and bad tips abound in the Javascript world.

These videos should help. β€œThe best of luck.”

+4
source

For anyone interested, I managed to get this to work with PhoneGap Build using the following:

 <gap:plugin name="cordova-plugin-network-information" version="1.0.1" /> <feature name="Geolocation"> <param name="ios-package" value="CDVLocation" /> </feature> 

No luck with the CLI, therefore, leaving this unanswered as it was an original question (and it would still be easier to get this working).

0
source

Source: https://habr.com/ru/post/1235676/


All Articles