detecting phonegap?

1,093 views
Skip to first unread message

Ed Anuff

unread,
Oct 4, 2010, 10:20:45 PM10/4/10
to phonegap
I've searched the group archives and, although the question has been
asked several times, there doesn't appear to be any good way to
determine whether you're running in the browser for testing purposes.
Is this still the case? The suggestion to not store phonegap.js on
the server isn't really a good approach. Looking for "Macintosh" in
navigator.userAgent can be used to determine if you're in desktop
Safari, but the userAgent string for phonegap and mobile safari are
identical in the simulator. TIA

Giacomo Balli

unread,
Oct 5, 2010, 3:48:41 AM10/5/10
to phonegap
When running in simulator, device.name and device.platform will reveal
it:

name":"iPad Simulator","platform":"iPad Simulator"

However, this is not really detecting PhoneGap... is it what you need?

Björn Nilsson

unread,
Oct 5, 2010, 7:33:54 AM10/5/10
to phonegap
"The suggestion to not store phonegap.js on
the server isn't really a good approach."

Why not?

Its what I do, and then check for what environment the JavaScript runs
in by checking if some PhoneGap function is present or not. Works
great so far.

I love this with JavaScript, that you can detect if stuff is available
or not. Makes it possible to run in very different environments with
the same code base.

Ed Anuff

unread,
Oct 5, 2010, 3:46:25 PM10/5/10
to phonegap
I appreciate that it works in practice, but it's really not an ideal
approach because (1) it does not address the issue of desktop local
testing - it's often highly desirable to test using the desktop
browser's javascript debugger and you're not going to want to move the
phonegap.js file just for this, (2) you get lots of spurious error
messages in your browser error console and web server logs, and (3)
it's prone to error, you might upload phonegap.js by mistake unless
you're using a scripted build and deploy mechanism, but then, why
should you layer another such mechanism when there are already such
mechanisms in your IDE?

For now, I've solved the problem by setting a global variable in the
webViewDidStartLoad method of PhoneGapDelegate.m, however, according
to http://stackoverflow.com/questions/3509098/ this might have
problems. On the plus side, modifying webViewDidStartLoad let me get
rid of the odd movie loading code that's in there right now.

If the idea of hiding the phonegap.js file is the way to go, then I'd
suggest adding a build phase script in the Xcode app target build at
the end that deletes the phonegap.js file from the www directory,
since at the beginning of the target build, there's already a build
phase script that copies a fresh copy of phonegap.js file into the www
directory from the PHONEGAPLIB directory. That way, there's much less
risk that a copy of phonegap.js gets copied onto a production web
server or loaded during desktop browser-based testing.

Ed

Dollar199

unread,
Dec 14, 2010, 1:40:43 AM12/14/10
to phon...@googlegroups.com
Any progress on this? I wanted to run onDeviceReady() in index.html when in desktop safari to make use of  web inspector. In other words I wanted to fire onDeviceReady() if PhoneGap was not detected. So I went with quick and dirty by using a timer to check if onDeviceReady() had already fired after one second. If it didn't, that meant that PhoneGap isn't available. To do this, edit the <script> in index.html:

    var phoneGapAvailable = false;
    var t = setTimeout("if (!phoneGapAvailable) onDeviceReady();", 1000);
   
    function onBodyLoad() {
        document.addEventListener("deviceready",onDeviceReady,false);
    }
   
    /* When this function is called, PhoneGap has been initialized and is ready to roll */
    function onDeviceReady() {
        phoneGapAvailable = true;
    }

It's only for debugging, so in production I just need to comment out the timer code line.But in the meantime onDeviceReady() fires in mobile safari, desktop safari, and inside phonegap. Think it does the job?

Dollar
Reply all
Reply to author
Forward
0 new messages