If you have set the UIWebViewDelegate
you can use the method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
Using this method we can block or allow certain links/urls to be opened in either the UIWebView
or in the mobile browser.
So we can do something like
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// We only want to block links
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *url = [[request URL] absoluteString];
if([url isEqualToString:@"http://www.google.co.uk"]) {
// If the requested url is a link and is http://www.google.co.uk block it from loading in the UIWebView and load it in the mobile browser.
if([[UIApplication sharedApplication] canOpenUrl:[request URL]]) {
// We should be checking to make sure we can open it first, for whatever reason we might not be able to and we don't want to give the user a bad journey.
[[UIApplication sharedApplication] openURL:[request URL]];
// We also need to tell our UIWebView not to do anything so we need to return NO.
return NO;
}
}
}
// If all is OK and we are happy for any other link to be included then just return YES and continue.
return YES;
}
If you want to block all links then just remove the isEqualToString:
if statement
or you can just block certain links by adding else if
to the if statement
. This code does one of two things it will either load the clicked link site in the UIWebView
or it will load the site in the mobile browser if it passes the if statements
.
UPDATE
It turns out the user isn't doing the objective-c
side of things they are doing the html
which wasn't in the original question, but since this could help users in the future I will leave my answer here.
EDIT
Since the user is using html
to try and direct the user to the mobile browser what they are looking for is called URL Schemes. Unfortunately I don't believe there is any URL Scheme as of iOS 7
that will open safari mobile browser.
This question has been asked before so I am not going to sit here and saying everything again I will just direct you to the question and answer Open Mobile Safari from a Link in a WebView