Oh UIWebView, how I adore you.

You know what’s better than UITextView?

UIWebView.

At least, if all you need to do is display stuff. If you need editing, of course UITextView is what you want. But I’m working on something that only needs to display text. I started out using UITextView because well… I need to display text, it’s only logical to use the text view (duh!). And while there’s some cool stuff in there, like data-detecting URL’s and automagically turning them into tappable hyperlinks that open up in Safari, it’s just not the same. Oh sure, you can read in an RTF file to have all sorts of spiffy formatting and layout, but I’m pulling my text out of an SQLite database. Lacking good SQLite editing tools or whipping up my own editor tool, plus wanting a way to have rich media depictions, I realized: HTML! I can have text, I can format it however I want, I can easily edit it in the lame SQLite tools available, I can have images, I can have tappable links, I could even put in movies, references from elsewhere online…. so many possibilities!

Another cool thing about UIWebView is it can show more than HTML. Take a look at Technical Q&A QA1630. You can show Excel, Keynote, Numbers, Pages, PDF, PowerPoint, Word, RTF, and RTFD documents. That’s pretty damn cool. But, HTML suffices for me because gosh… you’re running atop WebKit. HTML5, CSS, JavaScript, all sorts of really cool things are available to you, and all you have to do is hook up a UIWebView and load the page.

So really, if you just need to display text to an iOS user but want something richer than plain old text, UIWebView is so simple to use.

However…

You should do a few things to ensure a good experience.

First, set your UIViewController (typically, but use whatever is appropriate) to be the UIWebView’s delegate (and heed UIWebView’s documentation about its delegate property). Then have the delegate object respond accordingly:


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// If there's anything clickable, we don't want to show it inline; open up Safari to show it.
if (webView == self.myWebView)
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
}
return YES;
}

(Apologies for the crappy code formatting… wordpress.com’s editor seems to be doing funny things to code and I can’t get it to play nice). What this does is “redirect” any clicks upon links. As it is, if a user tapped on a link, that link would open up right there in the UIWebView, which may not be the desired user experience. So, using the above code you can redirect any link taps to an external web browser (Safari), and maintain your UIWebView as a pretty panel of stuff. Nice.

I’m in process of converting my UITextView’s to UIWebView’s. It’s a hassle, but it’s generating better results. I can keep all of my data as simple text streams, I can format it and have a high degree of control over the formatting, I can include rich media. What’s not to like?

2 thoughts on “Oh UIWebView, how I adore you.

  1. Hi, very nice post!a question about uiwebview(you adore it!): if i load a pdf i can’t use the hyperlinks inside it…i’m reffering to the hyperlinks to direct the user for example from the index to a chapter…do you know how to implement this?!?thank you!

Leave a Reply to hsoi Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.