Using a WebKit view for help

Since the F**KING NDA makes it difficult to find iPhone programming tips & sample code, I thought I’d share this little tidbit.

Sometimes the easiest way to display a help or information screen is a UIWebView. It’s very easy to display the contents of an HTML file stored in the application’s resource bundle.

    NSData *info = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"]];
    [webView loadData:info MIMEType:@"text/html" textEncodingName:@"utf8" baseURL:nil];

However, if you have any external links, they will open in the same web view, which you probably don’t want. I found a very nice work-around to have any clicked URLs open in Safari. You’ll need to specify a delegate for your web view that implements the webView:shouldStartLoadWithRequest:navigationType method. The code is very simple.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL: [request URL]];
        return NO;
    }
    return YES;
}

1 thought on “Using a WebKit view for help”

Leave a Comment