Monday, May 25, 2009

UIWebViewDelegate

I've been trying to figure out a few things about UIWebViewDelegate. In the process I noticed that a few of the people that try to put up code about it just aren't quite doing it 100%. So I thought that I would give it a go.

First of all, why would you want to use UIWebViewDelegate?
- to turn off the 'action' popup when people hold their finger over a graphic
- to show an activity indicator while your content is loading
- to catch certain loading errors for UIWebView

There are somewhat subtle things to do that really put some polish on your app. Unfortunately a lot of apps skip this, especially the first one.

Now, before I get into how to do it, I want to give credit where credit is due. I started from the example written up on http://www.iphonesdkarticles.com/2008/08/uiwebview-tutorial.html. They really did a good job with their article. But, naming their UIWebView object the way they did causes some conflict once you add the delegate.

Then, over at http://forums.pragprog.com/forums/83/topics/1450 is the best write up that I found on how to tie in the UIWebViewDelegate. Except they left out a couple of key details. Specifically that you need to add this line to viewDidLoad in order for webViewDidFinishLoad and other delegate methods to properly get called.

self.theWebView.delegate = self;

And then of course how to properly dealloc everything according to the doc.

theWebView.delegate = nil;
[theWebView release];

Now there are still probably some things that should be done to pretty things up if you were to release this to users. But that's not the point of today's exercise.

Here is a link to the source code. http://www.tweedleworks.com/download/WebViewDelegateTutorial.zip

No comments:

Post a Comment