Supporting Custom URLs in PhoneGap-iPhone apps pt 2 of 2

Supporting Custom URLs in PhoneGap-iPhone apps pt 2 of 2

My previous post explained how to get custom launch params so you can respond to different calling conditions, including a call from another app, or Mobile Safari. There is also a very good chance that your app may already be running, so what then?

Handle Open URL

With multitasking, this actually becomes quite likely, because users no longer kill your app, they simply move away from it. Your app is essentially suspended, so you should probably handle the non-launch option as well.

The ApplicationDelegate class defines a method just for this use, all you have to do is override it and hook it into your webView.

Copy the following to your AppDelegate.m and add the function to your js code:

Also, note that you can name your javascript function whatever you want, as long it is the same name that your AppDelegate passes into the webView. I chose to simply keep things clear and called the function "handleOpenURL(urlStr)", but it could just as easily be "appController.handleUrl(urlStr)". As in the previous post, this url is UrlEncoded, so you may need to decode it, depending on your needs.

One cool thing about the handleOpenURL function is that it is easily testable. You can put a link right inside your html to make the call using your protocol and it will be processed by the OS and passed right back to your app. This may not seem useful, but I can foresee situations where it might make sense, like passing info from a ChildBrowser control back to your main app, or completely decoupling your view from your app-controller. … Any other ideas?

I welcome your comments, questions, and suggestions. buy my single => 🙂

  1. Matt Pickell
    Matt Pickell08-22-2011


    I cannot get this to work. I’m using PhoneGap 1.0.0 and I see the invokeString code you wrote about in “Part 1 of 2” is in there, but this is not. I added it, and it couldn’t find a definition of webView, so i captured that as an instance var in the (void)webViewDidFinishLoad:theWebView: method.

    when i run it and test it, with the embedded link as you describe, I get a log message in Xcode saying “PhoneGapDelegate::shouldStartLoadWithRequest: Received Unhandled URL

    Any idea what i’m doing wrong?

  2. Matt Netkow
    Matt Netkow11-21-2011

    Hi Jesse – running into an issue with this. Using PhoneGap 1.2.0 and Xcode 4.2 (snow leopard). When my app reloads after coming back from a 3rd party Oauth call, it freezes. More details here. Any help would be greatly appreciated – thanks.

    No Twitter Messages