why do i get a nil value even if i give a url variable a value?

This seems to be an issue caused by downloadFile making an asynchronous call, I am not sure exactly why it doesn’t work but I managed to get it to work with the below solution where I skipped the completion handler and assigned the documentsUrl property inside the URLSession closure instead

So the do clause is changed to

do {
    try FileManager.default.copyItem(at: localURL!, to: destinationPath)
    documentUrl = destinationPath
}

To make the UI clearer I added a disabled operator to the second button so you can’t click it until the file has been downloaded

Button("Open PDFView") {
    ispresented.toggle()
}.disabled(documentUrl == nil)

Note that you can of course keep the closure if you want to do other stuff there but it wasn’t needed for this solution

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top