Mahrukh Salman
Mahrukh Salman

Reputation: 45

PDF file not showing ios

I have few pdf files at firebase server that i need to show to the users in the app. But the problem is, it doesn't show the pdf files neither in webview nor in WKWebView. I tried the following code snippets but neither worked.

if let url = URL(string: urlString){
  // First Approach          
   if let data = try? Data(contentsOf: url) {
       webView.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: url)
   }
            // Second Approach 
            let req = URLRequest(url: url)
            self.webView.load(req)
}

Here is one of the urls. Pdf file

I used the PDFKit as well but that also didn't work. Here is the log when i try to load it into webview.

2020-05-21 21:06:01.561593+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:10.383853+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:10.384063+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:13.050745+0500 Mini Muslims[41242:748634] Task <5CBC8251-89EC-4DCD-A9A0-E794B3AEB6EC>.<0> HTTP load failed, 0/0 bytes (error code: -999 [1:89])
2020-05-21 21:06:18.123494+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:18.123672+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:18.173099+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:18.173315+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:19.260676+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:19.260878+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:22.607212+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:22.607351+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:23.080709+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:23.080916+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:28.736644+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:28.736875+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO
2020-05-21 21:06:36.769000+0500 Mini Muslims[41242:748632] WF: _userSettingsForUser : (null)
2020-05-21 21:06:36.769220+0500 Mini Muslims[41242:748632] WF: _WebFilterIsActive returning: NO

The pdf never loads. What could be the possible issue. Please guide me so that i can resolve this issue.

Upvotes: 0

Views: 1417

Answers (1)

Use below code to load the pdf file in WKWebView

import UIKit
import WebKit

class ViewController: UIViewController,UIWebViewDelegate,UIScrollViewDelegate, WKUIDelegate {

   var WKwebView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
               let userContentController: WKUserContentController = WKUserContentController()
               let conf = WKWebViewConfiguration()
               conf.userContentController = userContentController
               WKwebView = WKWebView (frame: CGRect( x: 0, y: 60, width: self.view.frame.width, height: self.view.frame.height - 60 ), configuration: WKWebViewConfiguration())
               WKwebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
               WKwebView.uiDelegate = self
               WKwebView.scrollView.bounces = false
               WKwebView.scrollView.delegate = self;
               view.addSubview(WKwebView)

                var request : URLRequest!
                let PDFURL = "https://firebasestorage.googleapis.com/v0/b/mini-muslims.appspot.com/o/Books%2F-M5HgblfIM4jvINka3j2?alt=media&token=745ca3e3-b18b-4b2a-96c1-e791f977c2df";
                let url : URL = URL(string: PDFURL as String)!
                request = URLRequest (url: url);
                WKwebView.load(request)

    }
}

Upvotes: 1

Related Questions