import pdf from url

If I want create pdf to var dokumnetpdf from picker, var dokumnetpdf is empty. How I do that?

myCode:

 var dokumnetpdf = PDFDocument()

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

        guard let myURL = urls.first else {

            return

        }         //print("import result : (myURL)")          let url: URL = myURL         //print("PDF link:(myURL)")         pdfname = url.lastPathComponent        // print("PDF name: (pdfname)")        // textfield.text = pdfname         dokumnetpdf = PDFDocument(url: urls.first!)     }

Error LoG: import result : file:///private/var/mobile/Library/Mobile%20Documents/comappleCloudDocs/Desktop/rezervacefo.pdf

PDF link:file:///private/var/mobile/Library/Mobile%20Documents/comappleCloudDocs/Desktop/rezervacefo.pdf

PDF name: rezervacefo.pdf

doc:nil

AMC/DocumentUploadViewController.swift:68: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

2022-01-07 11:11:49.159474+0100 AMC[71388:8043348] AMC/DocumentUploadViewController.swift:68: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

Replies

PDFDocument(url:) returns an Optional value...
Try this:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let url = urls.first else {
        print("Error: no url")
        return
    }
    guard let dokument = PDFDocument(url: url) else {
        print("Error: could not create PDFDocument from url: \(url)")
        return
    }
    dokumnetpdf = dokument
}
  • in simulator is OK Thanks but in real device is not OK Error: could not create PDFDocument from url: file:///private/var/mobile/Library/Mobile%20Documents/comappleCloudDocs/Desktop/rezervacefo.pdf

  • Could there be a problem with your PDF file? Maybe try again, with a different PDF?

  • I've tried it on a few

in simulator iPhone iOS 12 pro is correct. This trouble is on real device iPhone 12 Pro.

  • Which is correct, your original code, or mine? What error do you see on a real device?

Add a Comment

It looks like a permissions issue.
Try this:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let url = urls.first else {
        print("Error: no url")
        return
    }
    guard url.startAccessingSecurityScopedResource() else {
        print("Error: could not access content of url: \(url)")
        return
    }
    guard let dokument = PDFDocument(url: url) else {
        print("Error: could not create PDFDocument from url: \(url)")
        return
    }
    dokumnetpdf = dokument
}
  • Error: could not create PDFDocument from url: file:///private/var/mobile/Library/Mobile%20Documents/comappleCloudDocs/Desktop/PDFTutorials/Vlozeni_skladove_polozky/Vlozeni_skladove_polozky.pdf

  • When accessing an iCloud pdf, it works for me, after adding "url.startAccessingSecurityScopedResource()"

  • Did you add the startAccessingSecurityScopedResource?