将缩略图附加到通知时,文件在哪里?

时间:2017-07-05 09:17:49

标签: ios swift notifications uilocalnotification

我正在为丰富的通知添加缩略图。我生成的图像用作这样的附件:

let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let fileURL = url.appendingPathComponent("someImageName", isDirectory: false).appendingPathExtension("png")
do{
    try UIImagePNGRepresentation(image)?.write(to: fileURL)
}catch{
    print("Could not write: ", error)
}

这很成功。如果我现在运行此代码:

let content = try? FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory())
print(content ?? "error")

打印出来:["someImageName.png"]

然后我创建附件,如下所示:

let attachment = try? UNNotificationAttachment(identifier: "image", url: url, options: nil)
let content = UNMutableNotificationContent()
content.title = "Test"
content.attachments = [attachment!]
let request = UNNotificationRequest(identifier: "someID", content:content, trigger: /*someTrigger*/)
UNUserNotificationCenter.current().add(request, withCompletionHandler:...)

这一切都有效。通知已发送,附件已附加。但是,如果我现在打印出临时文件夹的内容,它就是空的!我之前保存的图像已从此文件夹中删除。

经过大量的反复试验后,我发现在添加通知时图像已被删除。为什么我的通知会删除我的图片?我找不到任何关于此的文件!

如果我执行上述所有操作,但请注释掉此行://content.attachments = [attachment!],这意味着我的存储图像不会作为附件添加到我的通知中,那么我的图像不会从临时文件夹中删除!< / p>

这些文件在使用后是否被清理?或者在通知消失后我是否必须查看其他文件夹并手动清理它们?我不希望我的应用创建数百张永远不会再次使用的图片,而无法删除它们。

1 个答案:

答案 0 :(得分:1)

这些天我面临着同样的问题,答案在文档中,乍一看并不容易,但确实存在:

关于 UNNotificationAttachment

  

...通过验证后,附件文件将移入附件数据   存储,以便所有适当的人都可以访问它们   流程。复制位于应用程序包内的附件   而不是移动。

关于init方法:

  

安排包含附件的通知请求时,   附件的文件被移到新位置以方便访问   通过适当的程序。搬家后,进入的唯一途径   该文件使用UNUserNotificationCenter对象的方法。

所以我决定:

像您一样写文件并在NSTemporaryDirectory()中写一个文件的副本,我使用此示例轻松地创建和删除TempDirectory(Swift 4.0)(感谢Ole Begemman):

import Foundation

/// A wrapper around a temporary file in a temporary directory. The directory
/// has been especially created for the file, so it's safe to delete when you're
/// done working with the file.
///
/// Call `deleteDirectory` when you no longer need the file.
struct TemporaryFile {
    let directoryURL: URL
    let fileURL: URL
    /// Deletes the temporary directory and all files in it.
    let deleteDirectory: () throws -> Void

    /// Creates a temporary directory with a unique name and initializes the
    /// receiver with a `fileURL` representing a file named `filename` in that
    /// directory.
    ///
    /// - Note: This doesn't create the file!
    init(creatingTempDirectoryForFilename filename: String) throws {
        let (directory, deleteDirectory) = try FileManager.default
            .urlForUniqueTemporaryDirectory()
        self.directoryURL = directory
        self.fileURL = directory.appendingPathComponent(filename)
        self.deleteDirectory = deleteDirectory
    }
}

extension FileManager {
    /// Creates a temporary directory with a unique name and returns its URL.
    ///
    /// - Returns: A tuple of the directory's URL and a delete function.
    ///   Call the function to delete the directory after you're done with it.
    ///
    /// - Note: You should not rely on the existence of the temporary directory
    ///   after the app is exited.
    func urlForUniqueTemporaryDirectory(preferredName: String? = nil) throws
        -> (url: URL, deleteDirectory: () throws -> Void)
    {
        let basename = preferredName ?? UUID().uuidString

        var counter = 0
        var createdSubdirectory: URL? = nil
        repeat {
            do {
                let subdirName = counter == 0 ? basename : "\(basename)-\(counter)"
                let subdirectory = temporaryDirectory
                    .appendingPathComponent(subdirName, isDirectory: true)
                try createDirectory(at: subdirectory, withIntermediateDirectories: false)
                createdSubdirectory = subdirectory
            } catch CocoaError.fileWriteFileExists {
                // Catch file exists error and try again with another name.
                // Other errors propagate to the caller.
                counter += 1
            }
        } while createdSubdirectory == nil

        let directory = createdSubdirectory!
        let deleteDirectory: () throws -> Void = {
            try self.removeItem(at: directory)
        }
        return (directory, deleteDirectory)
    }
}

使用TempURL附加文件后,文件将被移动,您可以删除TempDirectory。

稍后进行了一些测试,可以确定的是,我终于发现我的文件(本例中为图像)存储在这里:

URL: file:///var/mobile/Library/SpringBoard/PushStore/Attachments/com.YourApp.Name/8e52c6673f6e735f83535486bf750ba1118a3ade.png

在展示或清洁通知中心之后,文件将被删除。

为什么所有这一切? ->沙箱!当我将文件附加到触发器(此处为UNCalendarNotificationTrigger)时,我将负责文件的日历命名为在通知中显示该日历,但要处理该文件,必须将该文件放在HIS目录中。

希望这会有所帮助,我花了一段时间才找到了所有的奥秘!