Check internet connection on iOS app with Cordova Phonegap 3.3.0 not working

我只是一个虾纸丫 提交于 2019-12-02 22:54:18

I finally solved the problem!! - by starting all over again from scratch and doing the following:

Command line:

sudo npm install -g cordova
cordova create hello com.example.hello HelloWorld
cd hello
cordova platform add ios
cordova platforms ls //This will list ios
cordova plugin add org.apache.cordova.network-information
cordova build

Then drag my files (HTML, Javascript etc) into the platforms/ios/www/ folder.

Open up hello.xcodeproj in xcode.

Edit config.xml and add the lines:

<feature name="NetworkStatus">
    <param name="ios-package" value="CDVConnection" />
</feature>

Then in my index file I used the JavaScript:

    <script type="text/javascript">
        document.addEventListener("deviceready", onDeviceReady, false);
        // device APIs are available
        function onDeviceReady() {
            if(navigator.network.connection.type == Connection.NONE){
                alert("nocon");
            }else{
                alert("yescon");
            }
        }
    </script>

Then run it in the iPhone / iPad simulator and it will output "yescon" if there is a connection and "nocon" if there isn't!!

Hope this helps!

Check if you have included your Cordova.js file in html.

<script type="text/javascript" src="cordova.js"></script>

And change App/Supporting Files/Cordova.plist of your project.

<key>Plugins</key>
     <dict>
     <key>NetworkStatus</key>
     <string>CDVConnection</string>
</dict>

This works for me:

if(navigator.network.connection.type == Connection.NONE){
//no connection
}else{
//You are connected.
}

Though I look in the documentation and looks like there's a difference using these lines:

var networkState = navigator.network.connection.type;

navigator.network.connection.type is set to Connection.CELL_2G for all cellular data.

Would it be var networkState = Connection.CELL_2G;?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!