Javascript – Phonegap – automatically including correct cordova

androidcordovagitiosjavascript

I'm developing a phonegap app on both iOS and android and have my www directory version controlled with git. I know that my HTML file needs to include the correct Cordova.js file (depending on which platform I'm currently developing on).

It is a little annoying pulling changes to www on iOS when someone was working on android. It gives me the endless gap_poll alert.

Simple solution is to remember to change the Cordova.js src so it points to the iOS version again. The problem with that is the other developers will need to keep changing their src if the latest commit was done on another platform.

Is there a way to automatically detect which version of Cordova to include? That way it would work on any platform and we wouldn't have to make tedious changes.

Best Answer

The following code works fine for me:

    function init() {
        if(isAndroid()){
            $("script").attr("src", "lib/android/cordova-1.7.0.js").appendTo("head");
        }else if(isiOS()){
            $("script").attr("src", "lib/ios/cordova-1.7.0.js").appendTo("head");
        }

         document.addEventListener("deviceready", onDeviceReady, false);
    }

    function isAndroid(){
        return navigator.userAgent.indexOf("Android") > 0;
    }

    function isiOS(){
        return ( navigator.userAgent.indexOf("iPhone") > 0 || navigator.userAgent.indexOf("iPad") > 0 || navigator.userAgent.indexOf("iPod") > 0); 
    }

    function onDeviceReady(){
        console.log("device is ready");
    }

You can check the full source code here