Skip to Navigation | Skip to Content



Supporting Custom URLs in PhoneGap-iPhone apps pt 2 of 2 | March 7th, 2011

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 => :)

Posted in iPhone, Objective-C, PhoneGap, XCode | 5 Comments » | Add to Delicious | Digg It

This entry was posted on Monday, March 7th, 2011 at 6:52 pm and is filed under iPhone, Objective-C, PhoneGap, XCode. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “Supporting Custom URLs in PhoneGap-iPhone apps pt 2 of 2”

  1. Preet Says:

    Hi,

    Hi usually read your posts and its quite helpful. I am developing an iPhone app using PhoneGap and Jquery Mobile. My app is quite heavy and takes around 15 secs to load. Could you please give me the solution that how can I add 2/3 splash images with time duration while loading an app. I have asked same ques in no. of forums but haven’t got any reply. Could you please suggest me something.

    Thanks.

  2. Ade Says:

    Thanks for the article. In iPhone, there needs to be a short timeout within the Javascript handleOpenURL function to allow the app to restore itself when returning from Safari.

  3. lucy Says:

    Hi, thank you for the nice post.

    I am using phonegap 1.0, Xcode 4.1 and iPhone 3G to test. I followed the instructions and the app.

    I changed “webView” to “self. webView” as received “Use of undeclared identifier ‘webView’ error.

    After the changes, it worked well from test link that added on the index.html. However, when launch the app from web site, process hangs at the [webView stringByEvaluatingJavaScriptFromString:jsString]; line in handleOpenURL method.

    Could you please suggest me something?

    Thank you.

  4. Jesse Says:

    Make sure you have the 7.1 version of the SDK, 7.0 and 7.1 are not compatible.

  5. Lucy Says:

    Hi Jesse,

    Thank you for your replay. I use OS X Lion, Xcode 4.1, Java SDK 1.6.0_26, and IOS SDK 4.3s and use iPhone 3G.

    I am not sure which SDK you are suggesting (SDK 7.1). Please advice…

    Thank you

Leave a Reply