Skip to content

Instantly share code, notes, and snippets.

@plasticmind
Created February 27, 2014 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plasticmind/9260407 to your computer and use it in GitHub Desktop.
Save plasticmind/9260407 to your computer and use it in GitHub Desktop.
UIWebView links open in Safari if the URL doesn't match a certain pattern
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ( navigationType == UIWebViewNavigationTypeLinkClicked ) {
NSString *urlString = request.URL.absoluteString;
// Set the pattern to search the request URL for
NSRange range = [urlString rangeOfString:@"mobile.example.com"];
if (range.location != NSNotFound) {
NSLog(@"%s", "This link matches the pattern and will be loaded in-app within the UIWebView.");
return YES;
} else {
NSLog(@"%s", "This link does NOT match the pattern and will be loaded in Safari.");
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
}
return YES;
}
// Documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#jumpTo_4
@plasticmind
Copy link
Author

Note: This should be placed in your ViewController file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment