I wanted to create an application which I could use to run my other PhoneGap applications for testing, a kind of wrapper or shell application. When you are developing a PG application, you are changing you code frequently and running the application again and again in the emulator could be time consuming (actually it is very slow for Android, but iOS is quite fast).
So this week I create a PhoneGap application which can take a URL of my PhoneGap application hosted on my development server and run it. The shell application is like a regular web browser where you enter the URL to open the page. However you cannot execute PhoneGap applications in a regular mobile browser (such as Safari and Chrome) because PhoneGap specific native libraries are not available.
So I can now create my PhoneGap applications, deploy it on a web server and open the link in my PhoneGap shell application. If I modify the application, I just refresh the page in the shell application. So testing is faster.
But the usefulness of this application is not just limited testing. It could be used as a client to PG mobile web applications, hosted on server in enterprise. You could create a home page of applications and open that page in the PG shell application. You can now have mobile web applications that can access Camera, Contacts, Files etc. of user’s mobile, using PhoneGap APIs. And adding new application or modifying application is simple, because you just add/update the application on your server and it is available immediately to users.
Here are the things you need to be aware of if you want to develop such application –
Separate cordova.js file for different platforms
Note : You need to be concerned about this only if you intend to test/support multiple mobile platforms.
When you create a PhoneGap application using the command line utility, it creates a project with cordova.*.js. The file name is same on Android and iOS (and possibly others too). But they are actually different JS files for each platform. The use-case I am talking about in this post requires you to host cordova.*.js on a server. You need to make sure that you host corodva.js on your server for all platforms you want to support.
So how does your web application know which corodva.js to load? I can think of two easy ways
- When you request a URL from the shell application, pass a URL parameter indicating which platform the application is going to run on. Then in your web (PG) application. load the platform specific cordova.js dynamically.
- Detect browser agent (HTTP_USER_AGENT) from the HTTP header request on the server (you will need an application server). The value of this header field contains text that can give you a hint about the mobile OS from where web request is coming. Then, as in #1 above, load appropriate cordova.js dynamically.
Same PhoneGap version on client and server
This might seem obvious, but I will state it anyway. You need to make sure that you include the version of cordova.js on the server (your web application), same as the one with which the shell application is created. For example, if the shell application is created with PhoneGap 2.5 then you need to include cordova.js of the same version.
PhoneGap applications do not work in frames
Once the web application is loaded in the shell application, I wanted to provide options to load other application urls, reload the page and go back to previous page – the basic browser options. So I though I would create buttons for these options in the shell application and load web application URL in an iFrame in the same page.
However I realized that PhoneGap does not fire deviceready event when the page is loaded in iFrame. Even using framesets did not work.
I worked around this problem in Android shell by providing application specific menus. However iOS does not support application menus. So in iOS it becomes the responsibility of web application to provide these (navigation) options, if they are required.
However if you type a wrong URL in the shell application, you will get ‘page not found’ error, then the only way to re-enter URL in the shell application in iOS is to kill the app and start again. In Android this is not an issue because the shell application provides application specific menu options to re-enter the URL.