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.

  • jhmac

    I have tired it many times starting from scratch. No errors no problems but opens google in safari not child browser. It has to be something simple. Any suggestions? Thanks for the post.

  • jhmac

    I have tired it many times starting from scratch. No errors no problems but opens google in safari not child browser. It has to be something simple. Any suggestions? Thanks for the post.

  • Jonathan

    Yup that happened to me to, just make sure you always add any domain that the ChildBrowser will open to the white list including redirects :)

  • Marco

    Thank you for the very helpful tutorial. Do you have any updates on loading local files?

  • eoinoc1

     Having trouble with the childbrowser and google analytics plugins. When I click an internal link trackpageview is triggered but once this happens the childbrowser pops up also. Any idea on a solution

  • eoinoc1

     Having trouble with the childbrowser and google analytics plugins. When I click an internal link trackpageview is triggered but once this happens the childbrowser pops up also. Any idea on a solution

  • Robert

    I’ve updated my stackoverflow post. As it turns out, what I encountered could be specific to my implementation, but if you still have a problem viewing local files, it may be worth checking it out. The source of my problem was the difference between the result of fileEntry.toURI() and what NSFileManager gives you when you when you build up a URI relative to the NSDocumentDirectory.

  • Pingback: Getting ChildBrowser plugin to work with PhoneGap 1.6

  • Thomas Bindzus

    Thanks, super guideline! I was able to find the correct plugins git repository and get my settings correct! I’m using Cordova 1.5.0 at the moment, but with the newest version of ChildBrowser, and that gave me a bit of a headache because the plugin didn’t work, until I found out that Cordova has changed from a first letter upper-case variable to an all lower-case variable somewhere between 1.5.0 and 1.6.1.

    Since I can’t upgrade to 1.6.1 at the moment (in production mode), I had to make a minor modification to the ChildBrowser.js file, it is really minor, all you have to do is insert this in the top of the file:

    // TB: This was added because Cordova v.1.5.0 uses Cordova while later versions use cordova
    var cordova = window.cordova || window.Cordova;

  • Thomas Bindzus

    Thanks, super guideline! I was able to find the correct plugins git repository and get my settings correct! I’m using Cordova 1.5.0 at the moment, but with the newest version of ChildBrowser, and that gave me a bit of a headache because the plugin didn’t work, until I found out that Cordova has changed from a first letter upper-case variable to an all lower-case variable somewhere between 1.5.0 and 1.6.1.

    Since I can’t upgrade to 1.6.1 at the moment (in production mode), I had to make a minor modification to the ChildBrowser.js file, it is really minor, all you have to do is insert this in the top of the file:

    // TB: This was added because Cordova v.1.5.0 uses Cordova while later versions use cordova
    var cordova = window.cordova || window.Cordova;

  • Thomas Bindzus

    Another very important thing to mention which might cause problems on your iDevice:

    First I used:

            
            It worked fine in the simulator, but when I deployed to my iPhone it didn’t work… the child browser didn’t open at all.Apparently iPhone’s file-system is case-sensitive (sorry perhaps that’s obvious to everyone else but me), so when I changed the script includes above to:

            
            Then it worked both in the simulator and on my iPhone, go figure why it didn’t fail both places…

  • http://www.facebook.com/profile.php?id=125158 Romy Mimi Ilano

    Thank you for posting this! It worked beautifully on Cordova 1.9 but I am having trouble with the new Cordova 2.0 
    Does anyone know if anyone generous has come out with a ChildBrowser for Cordova 2.0 plugin yet?

  • Shereef Marzouk

    This is how to do it on 2.0.0 http://stackoverflow.com/a/11721905/435706

  • Menno Bieringa

    Thank you Shereef Marzouk, but Randy McMillan just updated the plugin as you published your howto :)  

  • Kattis

    Thanks for a great tutorial! The best so far I’ve found on installing the ChildBrowser plug-in, I’ve trying to get this to work for hours, days, but finally it’s working :-)

    I think the biggest (and stupidest) mistake I did which was really hard find out was that I thought I added the key item pair ChildBrowserCommand ChildBrowserCommand in PlugIns, but I later on realized I had not pressed the arrow next to Plugins, so it wasn’t part of the plugin. This gave me the error:

    ERROR: Plugin ‘ChildBrowserCommand’ not found, or is not a CDVPlugin. 

    So all you others getting the same error, make sure the arrow next to Plugins in Cordova.plist (as it opens in Xcode) points down…

  • Hfqf123

    In appdelegate.m you can change the url of webview directly

  • Kamran_khan_68

    I am have problem with this example because my onDeviceReady is not working. I am using cordova2.0.0.
    I want to load the external URls in ChildBrowser. please help me and give me any clue. or link of any working example link.
    thanks in advance !!!!

  • Stephane Eyskens

    Hello,

    Thanks for this very detailed walkthrough, this helped a lot :) .

  • ranga reddy

    Hello, 
       
             Can anyone please guide me where to add this 
    exactly and how and from where to invoke child browser that shows google or some other website….Thanks to anyone who will be responding….

  • Henry Gomez

    Hi there, anyone knows how I could change the childbrowser modal view size. I  mean, I have a header and I need the childbrowser opens below it, not full sized.

  • Liu Han

    Hello, Guy.
    Thanks for your good example!.
    I have one more question. I want my app is available to read office word, excel document using phonegap.
    Do you know this solution?
    Regards.

  • Djmattski

    Worked a charm on Corova 2.1.0 – Just ensure to only copy the .m .h .bundle and .xib files across, not the whole folder.

  • Alberto Chavez

    This is awesome! It definitely helped me a lot :)

    Tested it with XCode 4.5.1 using Cordova 2.1.0
    XCode didn’t complain at all :)