PhoneGap: Detect if running on desktop browser

后端 未结 30 2725
时光取名叫无心
时光取名叫无心 2020-11-29 15:47

I\'m developing a web application that uses PhoneGap:Build for a mobile version and want to have a single codebase for the \'desktop\' and mobile versions. I want to be able

相关标签:
30条回答
  • 2020-11-29 16:07

    Aarons, try

    if (PhoneGap.available){
        do PhoneGap stuff;
    }
    
    0 讨论(0)
  • 2020-11-29 16:08

    Like the original poster, I'm using the phonegap build service. After two days and nearly 50 test builds, I've come up with an elegant solution that works great for me.

    I couldn't use UA sniffing because I wanted to test and run in mobile browsers. I had originally settled on cobberboy's quite functional technique. This didn't work for me because the "howPatientAreWe: 10000" delay/timeout was too much of a nuisance for in-browser development. And setting it any lower would occasionally fail the test in app/device mode. There had to be another way...

    The phonegap build service requires the phonegap.js file be omitted from your code repository before submitting your app's files to the service. Therefore I'm able to test for its existence to determine if running in a browser vs. app.

    One other caveat, I'm also using jQueryMobile, so both jQM and phonegap had to initialize before I could begin any custom scripting. The following code is placed at the beginning of my custom index.js file for the app (after jQuery, before jQM). Also the phonegap build docs say to place <script src="phonegap.js"></script> somewhere in the HTML. I leave it out completely and load it using $.getScript() to facility testing its existence.

    isPhoneGap = false;
    isPhoneGapReady = false;
    isjQMReady = false;
    
    $.getScript("phonegap.js")
    .done(function () {
        isPhoneGap = true;
        document.addEventListener("deviceready", function () {
            console.log("phonegap ready - device/app mode");
            isPhoneGapReady = true;
            Application.checkReadyState();
        }, false);
    })
    .fail(function () {
        console.log("phonegap load failed - browser only");
        isPhoneGapReady = true;
        Application.checkReadyState();
    });
    
    $(document).bind("mobileinit", function () {
        Application.mobileInit();
        $(document).one("pageinit", "#Your_First_jQM_Page", function () {
            isjQMReady = true;
            Application.checkReadyState();
        });
    });
    
    Application = {
        checkReadyState: function () {
            if (isjQMReady && isPhoneGapReady) {
                Application.ready();
            }
        },
        mobileInit: function () {
            // jQM initialization settings go here
            // i.e. $.mobile.defaultPageTransition = 'slide';
        },
        ready: function () {
            // Both phonegap (if available) and jQM are fired up and ready
            // let the custom scripting begin!
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:09

    I know it's been answered a while ago but "PhoneGap.available" doesn't exist anymore. You should use:

    if (window.PhoneGap) {
      //do stuff
    }
    

    or since 1.7, prefer:

    if (window.cordova) {
      //do stuff
    }
    

    EDIT 2019: as said in the comments, this only works if you do not include cordova lib into your desktop browser build. And of course it is a good practice to include only the strict minimum javascript/html/css files for each device you target

    0 讨论(0)
  • 2020-11-29 16:09

    I've actually found a combination of two of the techniques listed here has worked the best, firstly check that cordova / phonegap can be accessed also check if device is available. Like so:

    function _initialize() {
        //do stuff
    }
    
    if (window.cordova && window.device) {
        document.addEventListener('deviceready', function () {
          _initialize();
        }, false);
    } else {
       _initialize();
    }
    
    0 讨论(0)
  • 2020-11-29 16:11

    Detect desktop browser even if emulate device is active

    Works in Windows and Mac machines. Need to find a solution for linux View details

    var mobileDevice = false;
    if(navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|IEMobile/))
        mobileDevice = true; 
    
    if(mobileDevice && navigator.platform.match(/Win|Mac/i))
        mobileDevice = false; // This is desktop browser emulator
    
    if(mobileDevice) {
        // include cordova files
    }
    
    0 讨论(0)
  • 2020-11-29 16:13

    Just for info the way in PhoneGap 3.x Mobile Application Development Hotshot

    var userLocale = "en-US";
    function startApp()
    {
    // do translations, format numbers, etc.
    }
    function getLocaleAndStartApp()
    {
        navigator.globalization.getLocaleName (
            function (locale) {
                userLocale = locale.value;
                startApp();
            },
            function () {
                // error; start app anyway
                startApp();
            });
    }
    function executeWhenReady ( callback ) {
        var executed = false;
        document.addEventListener ( "deviceready", function () {
            if (!executed) {
                executed = true;
                if (typeof callback === "function") {
                    callback();
                }
            }
        }, false);
        setTimeout ( function () {
            if (!executed) {
                executed = true;
                if (typeof callback === "function") {
                    callback();
                }
            }
        }, 1000 );
    };
    executeWhenReady ( function() {
        getLocaleAndStartApp();
    } );
    

    and in YASMF framework

    https://github.com/photokandyStudios/YASMF-Next/blob/master/lib/yasmf/util/core.js#L152

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