0

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)"

Zilant
  • 57
  • 1
  • 10
  • Please share what string you are getting when pdf link is clicked. – Surbhi Garg May 25 '18 at 05:16
  • if ([string rangeOfString: @"openInSafari=true"].location != NSNotFound) replace this code with this if (string.lenght>0) –  May 25 '18 at 05:43
  • It works. But this solution doesn't suit me. I have links like **index.php?route=account/address/add** and this links open in Safari – Zilant May 26 '18 at 13:30
  • @SurbhiGarg I updated my question – Zilant May 26 '18 at 13:37
  • so you need to put a separate condition for external links also, because they will have download_id – Surbhi Garg May 28 '18 at 03:51
  • I found a solution for myself. I use **[string containsString: @"openInSafari=true"]** instead **[string rangeOfString: @"openInSafari=true"].location != NSNotFound**. It works great, but **containsString** сan be used only for **iOS 8** and later – Zilant Jun 08 '18 at 00:26

0 Answers0