Reputation:
I have an basic test application. It contains a UIWebView.
The website contains links with target="_blank" and some without it. The problem is that what happens is the opposite, the "_blank" open inside the app, and the others open in Safari.
My View controller contains this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return false;
}
return true;
}
How could it be? thanks
Upvotes: 1
Views: 1840
Reputation: 4268
You've implemented this behavior yourself. When you receive UIWebViewNavigationTypeLinkClicked
, it means that ANY link is clicked, not only links with target="_blank"
. For this case you invoke -[UIApplication openURL:]
, so, you open any links in Safari.
When target is "_blank", you will receive the same delegate method with UIWebViewNavigationTypeOther
. For this case, you return true
, so link is opened in web view. Problem here, that there is no easy way to differ this case from many "other" cases. UIWebView
API is far from perfect. I suggest to switch to WKWebView
.
See this question Open target="_blank" links outside of UIWebView in Safari for details about UIWebView
-based implementation of this functionality.
UPD: Checked again. For case with target="_blank"
, I receive 2 subsequent shouldStartLoadWithRequest:
calls, first with UIWebViewNavigationTypeLinkClicked
, second with UIWebViewNavigationTypeOther
. So, in my case all links opened in Safari with code from question. Checked on iOS 9.1 and iOS 8.4.
Upvotes: 1