I have a small app for iOS that uses WebView. I need links that contain target = "_blank" to open in Safari
I found a solution. But it does't work for me.
https://stackoverflow.com/a/15048074/4489534
All links are now opened in Safari, and all links containing "?openInSafari = true" too. But external(downloadable) files like PDF open in WebView.
I can't understand why the condition doesn't work
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = [request URL];
NSString *string = [url query];
if ([string rangeOfString: @"openInSafari=true"].location != NSNotFound){
[[UIApplication sharedApplication] openURL: url];
NSLog(@"Open in Safari");
return NO;
}
}
NSLog(@"Open in WebView");
return YES;
}
EDITED
When i click to link containing "?openInSafari = true", i get "Open in Safari openInSafari=true"
When i click to normal link, i get "Open in Safari (null)"
When i click to downloadable link to PDF file, i get "Open in WebView product_id=50&download_id=21"
When i click to direct link to PDF file, i get "Open in Safari (null)"