PhoneGap 2.0 Released

PhoneGap 2.0

PhoneGap Day Swag

PhoneGap (and Cordova) got their 2.0 release today at PhoneGap Day here in Portland. The project has been moving at an insane pace so 2.0 is a good chance to take a look at everything that’s been added over the past few releases. A quick list:

  • A common cordova.js file so the API is consistent across devices.
  • A command line interface for generating (and debugging/testing) projects.
  • Bada/Windows Phone 7 support.
  • CordovaWebView which lets you use PhoneGap within an app that uses the native UI framework.
  • Weinre for testing remotely on devices.

Lots of awesome stuff and with the way the project cranks out releases, there will be more to keep up with very soon.

PhoneGap 2.0.0rc1 – With Command Line Goodness

The PhoneGap team announced that PhoneGap (and Cordova) have reached release candidate one for 2.0.0. They’ll be doing the official 2.0.0 release in Portland at PhoneGap Day (go get the last few tickets if you haven’t already!) but you can grab RC1 and start checking it out.

One of the cool new shineys is that the team has added command line tooling making it easy to create and test Cordova applications. You can see some of how the command line works here in the Docs repo. You basically create the project with the ./create command and that will build a sample project for you and also generate scripts in your project for building and emulating the app.

So if you get a chance, pull down RC1 and give it a spin. Then come see all the hotness in action in Portland.

The PhoneGap Starter Project

As I’ve been digging more and more into PhoneGap Build I’ve also been discovering that there are some gaps in the workflow and that it’s not always easy to go from a feature I want to use to a cross-platform implementation for it. Most of the time I’ll use the PhoneGap documentation as a starter point and then try to wrangle it into a local project and then make that local project work with PhoneGap Build. One of my favorite features of PhoneGap Build is that it can pull directly from a git/svn repository and build binaries from that repo. So last night I did a little bit of work to pull a couple of features/code from the documentation into GitHub projects that can be quickly loaded into PhoneGap Build and turned into binaries. Fueled by some NW Peaks beer last night I built some initial ideas for the PhoneGap Starter Project. As you can see it’s pretty sparse but there are a few goals I have in mind.

  • Showing how to structure projects for use in PhoneGap build. I struggled with this a bit–with things like making the config.xml file work, setting the files up correctly (in a www folder, and referencing cordova.js in the script tag but not including it), etc. Each one of these projects works with PhoneGap Build out of the box.
  • Linking documentation to a real project. The documentation for PhoneGap is pretty good and I wanted an easy, out-of-the-box way for people to see the features in action. So I took the code from the documentation and turned them into projects. Load them into PhoneGap Build and you can see them in action on your device in a matter of seconds.
  • Helping fix documentation! I’m looking for ways to contribute to the Cordova project and some of the documentation code is a bit out of date. What better way than to go through that code, make sure it works, fix bugs, and then submit them back?
  • Showing how kick ass PhoneGap Build is. I really, really like PhoneGap Build and once you get a good workflow down I think it’s a great help in creating binaries quickly. This shows it in action.

To use the project in PhoneGap Build, just create a new app from SVN and put in the URL of the repo.

Adding an App to PhoneGap Build

Creating a new app based on Git/SVN project

I’m going to be filling out some of other documented features over the week as well as doing an example for the ChildBrowser plugin. I’m curious to hear if this seems useful for people or solves any kind of pain point. Since it helps me find the gaps/issues in the documentation, it serves a higher purpose for me, but if it seems useful then I’ll work on keeping it up to date.

Installing the ChildBrowser Plugin for iOS with PhoneGap/Cordova 1.5

Note: I’ve added a ChildBrowser example to the PhoneGap Starter project. It won’t help with native issues, but if you’re using PhoneGap Build it might give you a working head start.

I’ve seen a bunch of people who are new to PhoneGap/Cordova struggle with getting the ChildBrowser up and running. I fought with it today as well and most of my mistakes were based on old blog posts so I wanted to do a quick post that hopefully shows up in Google for people running into the same problems I had.

I’m starting with a blank project. The ChildBrowser plugin I’m using is here which as of this writing has been updated to support Cordova 1.5 with some potential backwards compatibility with 1.4.1. The simplest thing is to probably just download the source as a zip.

I’m starting with a blank project that has my www folder already referenced by the project. Here are the rest of the steps.

1. Drag all of the .m, .h, and .bundle file(s) from the ChildBrowser folder to the Plugins folder of your project. Make sure the copy option is selected (I think it’s the default).

2. In your project, under the Supporting Files folder, open the Cordova.plist file. You’ll see a bunch of key/value pairs and the one you want is under Plugins. You’ll need to add 2 by clicking the add button that shows when you highlight the Plugins section.

Key: ChildBrowser, Type:String, Value: ChildBrowser.js
Key: ChildBrowserCommand, Type:String, Value:ChildBrowserCommand

3. You also need to make sure that your application can call the external URL you want to load. This is also handled in the Cordova.plist file under ExternalHosts. Add any of the URLs you plan to open to that. You just need the Value, so you can leave the Key as the default. And remember that you can use wildcards, so the entry below lets you go to any subdomain of Google.

Key: Item 0, Type: String, Value: *.google.com

4. Copy over ChildBrowser.js (or the minified version) from the project files to your www folder.

Now you need to go into your HTML and JS files and make some changes. The biggest one is to reference the ChildBrowser.js file.

<script type="text/javascript"charset="utf-8"src="ChildBrowser.js"></script>

And here’s the modified JavaScript I used to get it to work. I created a childbrowser variable, then in onDeviceReady added a call to the install method of the ChildBrowser object. Finally I created a function that handles the URL opening.

    var childbrowser;
 
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
 
function onDeviceReady()
{
// do your thing!
childbrowser = ChildBrowser.install();
navigator.notification.alert("Cordova is working")
}
 
    function onLinkClick() {   
 
    if(childbrowser != null)
    {
        childbrowser.onLocationChange = function(loc){ alert("In index.html new loc = " + loc); };
        childbrowser.onClose = function(){alert("In index.html child browser closed");};
        childbrowser.onOpenExternal = function(){alert("In index.html onOpenExternal");};
 
        window.plugins.childBrowser.showWebPage("http://google.com");
 
    }  
}

This definitely works with Cordova 1.5 and I’m pretty sure it should work with 1.4. Notice that there’s no need to change any of the .m/.h files, so don’t worry about importing anything or changing code that you may have seen in other blog posts.

And I’m pretty sure this is basically how all Cordova/PhoneGap plugins work. Simply copy the OS-specific files into the plugin directory and then copy the JS files and reference them. But ChildBrowser threw me for a loop because of all the blog noise that referenced past versions. So hopefully this helps someone.