简体   繁体   中英

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

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

在此处输入图像描述

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM