How to Open a Link to a PDF with Wkwebview

How to open a Link to a PDF with wkwebview

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)

WkWebView does not load links to pdfs

There are a few things you can check:

  1. Verify that the <a href links doesn't contain target="_blank" attribute since WKWebView doesn't know how to open the link in a new tab. see https://stackoverflow.com/a/25853806/810466 for how to work around that.

  2. Check if the link is HTTPS or update the App Transport Security Settings with the Allow Arbitrary Loads option

  3. Make sure that you start the loading request only after adding the WKWebView to the view hierarchy in didMoveToParentViewController: since it may make javascript to fail if it tries to run outside the view hierarchy

  4. Implement the WKWebView NavigationDelegate methods and make sure you return WKNavigationActionPolicyAllow when deciding the policy for the request

WKWebView open PDF in external browser

You can do something like this

  extension ViewController: WKNavigationDelegate{
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print(navigationAction.request.url ?? "")
if let url = navigationAction.request.url?.absoluteString{

}
decisionHandler(.allow)
}
}

And don't forget to assign

 webView.navigationDelegate = self

The above method will get called when ever webview tries to load a new url and as per your comment and updated question you want to check the mime type of url. For that you can refer to

  1. This SO Answer

  2. Library

WKWebView not loading pdf

If you are directly trying to open the pdf from URL into WKWebView it will not work because of restriction added to WKWebView for device protection.

You can refer this article How to open a Link to a PDF with wkwebview for more information.

iOS 13 WKWebView not showing pdf file anymore

I figured out that the response' content-type was "application/octet-stream" instead of "application/pdf"

So I load the WKWebView as:

if let myURL = URL(string:"somefileurl.pdf") {
if let data = try? Data(contentsOf: myURL) {
webViewPdf.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: myURL)
}
}

Opening PDF files in WKWebView objective c

You don't need to do this:

TextToBeDisplayed = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&err];

Also in this method you'll get not NSURLRequest, but some kind of text from inside your PDF file trying to transform to NSURLRequest

NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:TextToBeDisplayed]];

All you need is this:

    NSString *filePath;
filePath = [[NSBundle mainBundle] pathForResource:@"Sample1" ofType:@"pdf"];

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];

[webView setBackgroundColor:[UIColor whiteColor]];

[webView loadRequest:nsrequest];
[self.view addSubview:webView];


Related Topics



Leave a reply



Submit