PDFKit's PDFDocument init(url: URL) does not work with HTTPS

Viewed 5349

When I try to use PDFDocument.init(url: ), I noticed that it worked with http but not with https URLs. Does anyone know why?

import PDFKit

let httpURL = URL(string: "http://www.axmag.com/download/pdfurl-guide.pdf")!
let doc1 = PDFDocument(url: httpURL) //makes a PDFDocument

let httpsURL = URL(string: "https://www.gnu.org/s/libmicrohttpd/tutorial.pdf")!
let doc2 = PDFDocument(url: httpsURL) //nil
2 Answers
@IBOutlet var pdfView: PDFView!

var pdfDOC: PDFDocument!

 override func viewDidLoad() {
        super.viewDidLoad()
        downloadPDF()
    }

  func downloadPDF(){
        let urlString = "https://www.tutorialspoint.com/swift/swift_tutorial.pdf"
        guard let url = URL(string: urlString) else {return}
        do{
            let data = try Data(contentsOf: url)
            pdfDOC = PDFDocument(data: data)
            pdfView.displayMode = .singlePageContinuous
            pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            pdfView.displaysAsBook = true
            pdfView.displayDirection = .vertical
            pdfView.document = pdfDOC
            pdfView.autoScales = true
            pdfView.maxScaleFactor = 4.0
            pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
        }catch let err{
            print(err.localizedDescription)
        }
    }
Related