【发布时间】: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.
已完成以下测试:
-
两个 URL 路径相同。
-
收到通知后我下载了容器,通知上出现了图片,但在文档目录中没有找到图片。
那么处理这种情况最好的方法是什么?
【问题讨论】:
-
您省略了从文件中移动和读取的部分。没有它就很难为您提供帮助。
标签: ios swift objective-c nsfilemanager unnotificationattachment