再會。
我收到一條帶有圖片 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 路徑是相同的。
我在收到通知后下載了容器,通知上出現了影像,但在檔案目錄中沒有找到影像。
那么什么是最好的處理這種情況呢?
uj5u.com熱心網友回復:
但是在檔案目錄中沒有找到圖片
因為你沒有保存到檔案目錄。downloadTask是易變的。一旦您從完成處理程式回傳,它下載檔案的 URL 就會被洗掉。如果您不想丟失它,則必須將其復制到更安全的位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387395.html
標籤:ios 迅速 目标-c nsfilemanager 取消通知附件
