Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  182] [ 4]  / answers: 1 / hits: 81695  / 13 Years ago, wed, november 9, 2011, 12:00:00

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 to detect if PhoneGap calls will work (ie, is the user on a mobile device that will support PhoneGap).



I've searched and cannot believe there is no simple way of doing this. Many people have offered suggestions;





None of which work, unless you remove the PhoneGap Javascript file from the desktop version of the app, which defeats my goal of having one codebase.



So far the only solution I have come up with is browser / user agent sniffing, but this is not robust to say the least. Any better solutions welcome!



EDIT: A marginally better solution is to try calling a PhoneGap function after some small timeout - if it doesn't work, then assume the user is on a desktop web browser.


More From » cordova

 Answers
4

I use this code:



if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
document.addEventListener(deviceready, onDeviceReady, false);
} else {
onDeviceReady(); //this is the browser
}


UPDATE



There are many other ways to detect if phonegap is running on a browser or not, here is another great option:



var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
if ( app ) {
// PhoneGap application
} else {
// Web page
}


as seen here: Detect between a mobile browser or a PhoneGap application


[#89221] Tuesday, November 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raveno

Total Points: 453
Total Questions: 92
Total Answers: 92

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
raveno questions
;