Atif Khan
Atif Khan

Reputation: 321

iOS 13 WKWebView not showing pdf file anymore

I am using WKWebView to show a pdf file from a remote url. It was working fine in iOS 12 but in iOS 13 it just shows blank screen. I hit same domain with an image url and that worked fine but it has some issues with pdf files only.

let myURL = URL(string:"somefileurl.pdf") // If I hit this url in safari, It will download a pdf file.
let myRequest = URLRequest(url: myURL!)
webViewPdf.load(myRequest)  

Upvotes: 8

Views: 12583

Answers (4)

avish94
avish94

Reputation: 1

Just Implement the decidePolicyFor method available in WKNavigationDelegate as given below,

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
   decisionHandler(.allow)
}

and set the delegate in your web view as given below,

yourWebView.navigationDelegate = self

Upvotes: 0

Hanbing Yin
Hanbing Yin

Reputation: 51

UIWebView also has the same problem. Fixed as follow(objective-c):

[self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"" baseURL:[NSURL URLWithString:@"FilePathOrUrlString"];

Upvotes: 0

Atif Khan
Atif Khan

Reputation: 321

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

Upvotes: 16

Amul4608
Amul4608

Reputation: 1448

For PDF Try This in My Case Working 100%

func openFile(url:URL){
self.displayDocument(filePath: url, type: "pdf")
}



 extension Your ViewControler Name :UIDocumentInteractionControllerDelegate{

            public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
                UINavigationBar.appearance().tintColor = UIColor.blue
                return self
            }

            public func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
                UINavigationBar.appearance().tintColor = UIColor.clear
                self.documentInteractionController = nil
            }
            func documentInteractionControllerDidDismissOptionsMenu(_ controller: UIDocumentInteractionController) {
                UINavigationBar.appearance().tintColor = UIColor.clear
                self.navigationController?.dismiss(animated: false, completion: nil)
            }

            func displayDocument(filePath:URL,type:String){
                SVProgressHUD.dismiss()

                self.documentInteractionController = UIDocumentInteractionController(url:filePath )
                // Preview PDF
                self.documentInteractionController.delegate = self
                self.documentInteractionController.presentPreview(animated: true)
            }
        }

Upvotes: -1

Related Questions