How to intercept navigation in a UIWebView

UIWebView is a great control to display formatted text (using HTML language). That text might contain links that you want to intercept in Objective-C and do a specific action (push a new view for example).

You can do this by implementing UIWebViewDelegate protocol…

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
	NSArray *parts = [[[request URL] absoluteString] componentsSeparatedByString:@"#language_id_"];
	if ([parts count] == 2) {
		TranslationViewController *vc = [[TranslationViewController alloc] initWithNibName:@"PrayerTranslationView" bundle:nil];
		[self presentModalViewController:vc animated:YES];
		[vc release];
                return FALSE;
	}
	return TRUE;
}

In that method you can check the requested URL and decide if you want to allow the standard flow, navigation to that URL (by returning TRUE),  or do something else instead. In the example above we are showing a new view.