Created
February 27, 2014 21:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This should be placed in your ViewController file.