【问题标题】:Unable to access Files, because there is no such file or directory无法访问文件,因为没有这样的文件或目录
【发布时间】:2022-01-15 15:02:54
【问题描述】:

美好的一天。

我收到一条带有图片 URL 的消息。每当我收到图像 URL 时,我都必须通过本地通知显示它。像这样

但是,我使用UNNotificationAttachment

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

在哪里提到

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.

但是,当我收到消息时,我会先下载它然后触发本地通知

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()
}

它成功返回本地URL,我可以成功显示通知。

我已经保存了最后一个路径,因为我已经下载了图像。我不想再下载了。

但是每当我打开应用程序并想要访问下载的文件时,它就会出现错误

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

已完成以下测试:

  1. 两个 URL 路径相同。

  2. 收到通知后我下载了容器,通知上出现了图片,但在文档目录中没有找到图片。

那么处理这种情况最好的方法是什么?

【问题讨论】:

  • 您省略了从文件中移动和读取的部分。没有它就很难为您提供帮助。

标签: ios swift objective-c nsfilemanager unnotificationattachment


【解决方案1】:

但是在文档目录中没有找到图片

因为你没有把它保存到文档目录。 downloadTask 不稳定。一旦您从完成处理程序返回,它下载文件的 URL 就会被删除。如果您不想丢失它,则必须将其复制到更安全的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-26
    • 2014-01-16
    • 2020-08-18
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多