Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Feb 27, 2014

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
You can’t perform that action at this time.