navigator.connection.type not working even if device is ready *or* device is never ready

前端 未结 2 702
小鲜肉
小鲜肉 2021-01-15 00:29

I\'m trying to make a simple app with Phonegap, compiled with Adobe Phonegap builder. I\'ve found and used the well documented example for using navigator.connection.type wh

相关标签:
2条回答
  • 2021-01-15 00:42

    You should also wait until all your scripts are loaded. Wrap everything in a onBodyLoad like so:

    function onBodyLoad() {
        // these are useful later in the app, might as well set early
        window.isRipple = (window.tinyHippos != null);
        window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href);
        window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null;
        window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null;
    
        // stuff I use for debugging in chrome
        if (window.isPhoneGap) {
            document.addEventListener("deviceready", onDeviceReady, false);
        } else {
            onDeviceReady();
        }
    }
    

    And add to your body tag:

    <body onload="onBodyLoad()"> 
    

    And the rest of my code for additional references:

    function checkOffLine(minutes) {
        if (window.lastCheckTime == null) {
            window.lastCheckTime = 0;
        }
    
        var currentTime = (new Date()).getTime();
        if (currentTime < (window.lastCheckTime + minutes * 60000)) return;
        window.lastCheckTime = currentTime;
    
        // ios does not allow you to exit the application so just warn
        // ios also require you to warn or your app get rejected
        if (window.isIOS) {
            navigator.notification.alert('This application may not function properly without an internet connection.');
        } else {
            navigator.notification.confirm(
                'This application may not function properly without an internet connection.  Continue working offline?', // message
                function(button)      // callback to invoke with index of button pressed
                {
                    if (button == 1) {
                        navigator.app.exitApp();
                    }  
                },
                    'Warning', // title
                    'Exit,Continue'                     // buttonLabels
                );
            }
    }
    
    function checkConnection() {
        // your check connection type code here or just use navigator.onLine
        if (!navigator.onLine) {
            // don't be annoying, only confirm for once every every 5 minutes
            checkOffLine(5);
        }
    }
    
    // initial phonegap deviceready handler
    function onDeviceReady() {
        console.log('Application started');
        angular.bootstrap(document, ['assetsApp']);
        if (window.isPhoneGap) {
            document.addEventListener("offline", checkConnection, false);
            checkConnection();
        }
    };
    
    function onBodyLoad() {
        // these are useful later in the app, might as well set early
        window.isRipple = (window.tinyHippos != null);
        window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href);
        window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null;
        window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null;
    
        // stuff I use for debugging in chrome
        if (window.isPhoneGap) {
            document.addEventListener("deviceready", onDeviceReady, false);
        } else {
            onDeviceReady();
        }
    }
    
    0 讨论(0)
  • 2021-01-15 00:58

    I had the same issue and found I had to run "cordova build" and then the status was returned correctly.

    BEWARE When I run cordova build, it appears to take everything in my ~/app/www directory and overried everything in app/platforms/android/assets/www/

    My "install process" is as follows:

    cordova create app com.app "App"
    cd app
    cordova platform add android
    cordova plugin add org.apache.cordova.network-information
    cordova plugin add org.apache.cordova.camera
    cordova plugin add org.apache.cordova.geolocation
    cordova build
    

    I can then do code changes in app/www and when happy, 'deploy' it using 'cordova build' (which seems to always copy the files to app/platforms/android/assets/www/.

    If I add another plugin using: (for example)

    cordova plugin add org.apache.cordova.file
    

    then I need to run

    cordova build
    

    to have it work.

    I hope this helps

    (I am using cordova 3.3.1-0.1.2 )

    0 讨论(0)
提交回复
热议问题