PDFView does not display my PDF
The problem is that the pdf you are importing it is just a temporary file. You need to copy/move the temporary file url to your app documents folder.
UIDocumentPickerModeImport The URL refers to a copy of the selected
document. This document is a temporary file. It remains available only
until your application terminates. To keep a permanent copy, you must
move this file to a permanent location inside your sandbox.
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
try FileManager.default.moveItem(at: url.standardizedFileURL, to: documentDirectory.appendingPathComponent(url.lastPathComponent))
tableView.reloadData()
} catch {
print(error)
}
}
PDF not shown correctly
Yeah, PDFView is annoying like that. set the PDFView's document to nil before assigning it a new document:
_pdfView.document = nil;
_pdfView.document = pdf;
This will reset it and solve your problem.
Also, you don't need to set needsDisplay on the window's view.
Cannot view PDF in PDFView IOS downloaded using Alamofire - SWIFT
PDFDocument(url: URL)
only accepts a file URL
, but in your code you say:
let fileURL = URL.init(string: self.containerPDFUrl)
let pdfDoc = PDFDocument.init(url: fileURL!)
You say that self.containerPDFUrl
is of the type
https://example.org/rab1Mpub.pdf
But that is not a file url. I believe you intended:
let pdfDoc = PDFDocument(url: self.destination.0)
But let fileURL = documentsURL.appendingPathComponent("pig.png")
should likely be let fileURL = documentsURL.appendingPathComponent("pig.pdf")
Update
if response.error == nil, let pdfFilePath = response.destinationURL?.path {
print("inside DispatchQueue")
print("pdfFilePath: \(pdfFilePath)")
DispatchQueue.main.async{
// Instantiate PDFDocument
let docURL = URL.init(string: pdfFilePath)
let pdfDoc = PDFDocument.init(url: docURL!)
self.myPDFView.document = pdfDoc
}
}
There's no need to deal with paths here. Simply take the destinationURL
and init the document from that. For example:
if response.error == nil, let pdfURL = response.destinationURL? {
DispatchQueue.main.async{
// Instantiate PDFDocument
let pdfDoc = PDFDocument(url: pdfURL)
self.myPDFView.document = pdfDoc
}
}
Related Topics
Iphonex Not Call Prefersstatusbarhidden
Using Uiapplicationdelegateadaptor to Get Callbacks from Userdidacceptcloudkitsharewith Not Working
Alamofire Type 'Parameterencoding' Has No Member 'Url' Swift 3
How to Prevent Timer Slowing Down in Background
What Is Trailing Closure Syntax in Swift
Detect Left and Right Click Events on Nsstatusitem (Swift)
How to Use Trailing Closure in If Condition
Physicsbody: Could Not Create Physics Body
Swift: Double Conversion Inconsistency. How to Correctly Compare Doubles
Swiftui: How to Make Entire Shape Recognize Gestures When Stroked
Session.Datataskwithurl Completionhandler Never Called
Why Does Int(Float(Int.Max)) Give Me an Error
How to Detect and Make Clickable Links in a Uilabel Not Using Uitextview