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

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 iPhone iOS 12 pro is correct. This trouble is on real device iPhone 12 Pro.

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
}

Try this for download iCloudFile:


url.startAccessingSecurityScopedResource()

if !FileManager.default.fileExists(atPath: url.path), FileManager.default.isUbiquitousItem(at: url) {

                try FileManager.default.startDownloadingUbiquitousItem(at: url)

                startReadingStatus(for: url) { isSuccess in
                    if isSuccess {
                        do {
                            try FileManager.default.copyItem(at: url, to: LOCAL_URL)
                        } catch {
                            print(error)
                        }
                    }
                    url.stopAccessingSecurityScopedResource()

                }
                return true

            }
    internal func startReadingStatus(for url: URL, completion: ((Bool) -> Void)? = nil) {
        DispatchQueue.main.async {
            Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { [weak self] (timer) in
                guard let status = self?.downloadStatus(for: url)
                else {
                    completion?(false)
                    return
                }
                if status == URLUbiquitousItemDownloadingStatus.current {
                    timer.invalidate()
                    completion?(true)

                }
            })
        }

    }

    
    internal func downloadStatus(for url: URL) -> URLUbiquitousItemDownloadingStatus? {
        do {
            let attributes = try url.resourceValues(forKeys: [URLResourceKey.ubiquitousItemDownloadingStatusKey])
            guard let status: URLUbiquitousItemDownloadingStatus = attributes.allValues[URLResourceKey.ubiquitousItemDownloadingStatusKey] as? URLUbiquitousItemDownloadingStatus
            else {
                return nil
            }
            return status

        } catch {

            return nil

        }
    }

import pdf from url
 
 
Q