Skip to content →

Open Link in Safari

Here some sample code showing you how to open a link in Safari when clicked on in your UIWebView. The method shouldStartLoadWithRequest is a delegate method that is called when a link is clicked on. You can override the method and tell it to open in Safari.

– (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType; {

NSURL *requestURL = [ [ request URL ] retain ];
// Check to see what protocol/scheme the requested URL is.
if ( ( [ [ requestURL scheme ] isEqualToString: @”http” ]
|| [ [ requestURL scheme ] isEqualToString: @”https” ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
// Auto release
[ requestURL release ];
// If request url is something other than http or https it will open
// in UIWebView. You could also check for the other following
// protocols: tel, mailto and sms
return YES;
}

Published in Code iOS