How to open a Link to a PDF with wkwebview

18,988

Solution 1

SWIFT 3.* & 4.* *

First you have to download that pdf file into your app, after downloading you have to get that file path, then that file path should be use like following way in WKWebView.

let fileURL = URL(fileURLWithPath: filePathURLData as! String)
//print(fileURL)
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)

Here filePathURLData is your actual file path which you have downloaded into your app, you have to convert this into URL, then you need to load that file into WKWebView

Thanks

Hope this will help you.

This will show any file in the WKWebView (doc, docx, xlsx, pdf, google doc, pages & Any textfile)

Solution 2

Likely, you are using target="_blank" in your anchor tag. That opens up a new window to display the link. WKWebView is blocking your attempt to open a new window (at least by default).

The code below still does not create a new window, but instead opens the PDF, etc link in the current WKWebView. The other option seems to be to create a new WKWebView and return it, so the ios and open the link in that, but I don't want extra Views being created by every click on a website inside the WKWebView.

In your ViewController.viewDidLoad

webView.uiDelegate = self

Then add the extension for the delegate

extension ViewController: WKUIDelegate {

    /**
     * Force all popup windows to remain in the current WKWebView.  
     * By default, WKWebView is blocking new windows from being created
     * ex <a href="link" target="_blank">text</a>.
     * This code catches those popup windows and displays them in the current WKWebView.
     */
    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {

        // open in current view
        webView.load(navigationAction.request)

        // don't return a new view to build a popup into (the default behavior).
        return nil;
    }
}

Solution 3

Objective C:

 NSURL *fileUrl = [[NSURL alloc] initWithString:filePathURLData];
[webView loadFileURL:fileUrl allowingReadAccessToURL:fileUrl];

Another way to open PDF saved in document directory:

 NSData *data = [NSData dataWithContentsOfFile:fileURL];
[webView loadData:data MIMEType:@"application/pdf" characterEncodingName:@"" baseURL:[NSURL URLWithString:fileURL]]; 

Solution 4

I have same issue, and found out the pdf file is using unsecured http. Therefore the app refuse to open it. Try to check with https link and see if it works.

Also with WKWebView, you don't need to download the pdf file before open it. Just load the url directly, i.e

webView.load(URLRequest(url: URL(string: "https-pdf-link")!))
Share:
18,988
Mondy
Author by

Mondy

Updated on September 27, 2022

Comments

  • Mondy
    Mondy over 1 year

    I created a simple iOS app, which opens a URL with WKWebView. On the website, there is a link to a PDF Document. When I open the site on my browser, I can click onto the Link, and the PDF document opens. But on my App, nothing happens when I click onto the link.

    How can I fix it? Do I have to put something into my info.plist?

  • Mondy
    Mondy over 6 years
    Into the website, there are more than one links to pdf documents, and I don't know, which links are used.
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @Mondy Whatever link you want to show in your WKWebView you have to download it first.
  • Mondy
    Mondy over 6 years
    But it is not possible, if I don't know the link. I redirect to an external site, which includes one or more links. Is it possible to let it open into the standard browser out of the app?
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @Mondy I think it can be open in safari, chrome or any other web browser but to open file in your app within WKWebView you have follow the above.
  • Mondy
    Mondy over 6 years
    Do you have an idea, how it can be open the link with standard browser?
  • Code Wiget
    Code Wiget over 4 years
    Is there no way to open a link without knowing about it before...? So if I use a wkwebview to open a page, and that page has a link to a pdf on it, there's nothing you can do...?
  • Yogendra Patel
    Yogendra Patel almost 4 years
    Thanks, your answer help me. Now i want to download that pdf how can i do that?