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
I wrote a post about it a few days ago. This is the best solution you can find (until PhoneGap will release something, maybe or maybe not), it's short, simple and perfect (I've checked it in every possible way and platform).
This function will do the job for 98% of the cases.
/**
* Determine whether the file loaded from PhoneGap or not
*/
function isPhoneGap() {
return (window.cordova || window.PhoneGap || window.phonegap)
&& /^file:\/{3}[^\/]/i.test(window.location.href)
&& /ios|iphone|ipod|ipad|android/i.test(navigator.userAgent);
}
if ( isPhoneGap() ) {
alert("Running on PhoneGap!");
} else {
alert("Not running on PhoneGap!");
}
To complete the other 2% of the cases, follow these steps (it involves a slight change on native code):
Create a file called __phonegap_index.html, with the source:
Now, on native simply change the start page from index.html to __phonegap_index.html on all your PhoneGap platforms. Let's say my project name is example, the files you need to change are (as for PhoneGap version 2.2.0):
CordovaLibApp/AppDelegate.msrc/org/apache/cordova/example/cordovaExample.javaexample/package.appxmanifestwww/config.xmlframework/appinfo.jsonsrc/WebForm.cpp (line 56)Finally, you can use it anywhere on your site, if it's running on PhoneGap or not:
if ( localStorage.getItem("isPhoneGap") ) {
alert("Running on PhoneGap!");
} else {
alert("Not running on PhoneGap!");
}
Hope it helps. :-)