Friday, January 30, 2009

How to debug handleOpenURL

I've seen a number of questions on the Apple iPhone developer forums asking how to debug the UIApplication handleOpenURL message. I finally had a complex scenario that I needed to debug, and came up with the following solution. Note that this example has been tested using the simulator only. Firstly, you'll need to grab the two 'DebugSupport' files from my google code repository here. Include them in your project and modify your handleOpenURL message as follows:
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
 [DebugSupport waitForDebugger];
 // place breakpoint after the above line
The call to [DebugSupport waitForDebugger] shows a UIAlertView, which will wait until you confirm by clicking the OK button. You'll notice that the prompt tells you the process ID. Don't click OK yet. Return to XCode and from the Run menu, choose Attach To Process | Process ID... Enter the PID given to you from the alert box and XCode will attach and enable all the breakpoints. Obviously, remove this from production code. Enjoy.


GregM said...

This is a lifesaver. Thank you so much for doing this!

Anonymous said...

That's very useful. Thanks.

One thing, though. When I tried to add your call

[DebugSupport waitForDebugger]

To my handleOpenURL method, My app got killed by the springboard for taking too long to respond.

What I did to fix that was to change my application:handleOpenURL: method. It now simply invokes a new instance method, myOpenURL after a delay. My application:handleOpenURL: method does this:

[self performSelector: @selector(myOpenURL:) withObject:url afterDelay: 0];

That way the handleOpenURL method returns right away, and I have time to attach the debugger and click the ok button in your alert.

powerpodz said...

Thanks ever so much for this.

First time I used it it worked perfectly.

However, now it comes up with a small problem in that it hangs just as the Alert box is about to get displayed i.e you see it fade in and around the simulator, but the dialog never comes up, so you can never get to debug it.

Any ideas?