Home > Enterprise >  Unable to access Files, because there is no such file or directory
Unable to access Files, because there is no such file or directory

Time:12-20

Good Day.

I receive a message with an image URL. Whenever I receive the image URL I have to show it through local notification. Like this

enter image description here

However, I use UNNotificationAttachment

convenience init(identifier: String, 
             url URL: URL, 
         options: [AnyHashable : Any]? = nil) throws

where it is mentioned that

The URL of the file you want to attach to the notification. 
The URL must be a file URL and the file must be readable by the current process. 
This parameter must not be nil.

However, when I receive the message, I download it first and then fire the local notification

func downloadImage(from remoteUrl: URL, completion: @escaping(URL?) -> Void) {
     URLSession.shared.downloadTask(with: remoteUrl) { localURL, response, error

       //move to the directory and return the URL
         
       completion(document directory path I have saved)
     }.resumeTask()
}

It successfully returns the local URL, and I am able to show the notification successfully.

I have saved the last path as I have already downloaded the image. I don't want to download it again.

But whenever I open the app and want to access the downloaded file, it appears with an error

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. No such file or directory. 

The following tests are done:

  1. Both URL path is the same.

  2. I downloaded the container after receiving the notification, Image is appeared on the notification but didn't find the image in the document directory.

So what is the best to handle the situation?

CodePudding user response:

but didn't find the image in the document directory

Because you didn't save it to the document directory. downloadTask is volatile. The URL it downloads the file to is deleted as soon as you return from the completion handler. If you don't want to lose it you must copy it to a safer location.

  • Related