【问题标题】:Importing Documents from iMessage into App - Deleting temp files placed in "Inbox"将 iMessage 中的文档导入 App - 删除“收件箱”中的临时文件
【发布时间】:2019-10-31 15:40:15
【问题描述】:

我已经为自定义文档格式定义了一个 UTI。我可以从我的应用程序导出文件并将它们附加到短信、电子邮件等。我可以通过点击 iMessage 中的文档图标将文件导入我的应用程序。通过点击文档图标,我可以选择复制到我的应用程序。这会在我的 AppDelegate 中触发一个调用来处理传入的文件。

困扰我的是传入文件的网址是:

file:///private/var/mobile/Containers/Data/Application/21377C94-1C3C-4766-A62A-0116B369140C/Documents/收件箱/...

然而,当将文档保存到 .documents 目录时,我使用这个 URL:

file:///var/mobile/Containers/Data/Application/21377C94-1C3C-4766-A62A-0116B369140C/Documents/...

区别在于/private//Inbox/ 路径组件。

问题:如何清除从 iMessage 复制到我的应用程序的文件的 /private/.../Inbox/ 路径?我在测试我的应用程序时注意到了这一点,当我在 iMessage 中点击相同的文档图标时,它开始生成具有相同名称的文件副本,但添加 -1,然后是 -2,然后是 -3 到文档的文件名信息。 /private/.../Inbox/ 路径中似乎正在建立副本。

这是自己刷新的东西还是我可以访问该目录以删除这些文件?这也很烦人,因为根据文件名,它似乎是一个不同的文件,因此允许以稍微不同的文件名导入同一文件的多个副本。

【问题讨论】:

    标签: ios swift xcode url uti


    【解决方案1】:

    好的,这需要大量的挖掘,但我会发布我的解决方案,到目前为止似乎有效,以防万一有人遇到同样的问题。

    let fileManager = FileManager.default
    // get the URL for the "Inbox"
    let tmpDirURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("Inbox")
    // get all the files in the "Inbox" directory
    let anythingThere = try? fileManager.contentsOfDirectory(at: tmpDirURL, includingPropertiesForKeys: nil)
    if anythingThere?.count ?? 0 > 0 {
       for eachURL in anythingThere! {
           // for each url pointing to a file in that directory, get its path extension
           let pathExtension = eachURL.pathExtension
           // test to see if it's a UTI that you're interested in deleting
           // in my case, the three "OCC" strings are the relevant UTI extensions
           if pathExtension == "OCCrcf" || pathExtension == "OCCrdi" || pathExtension == "OCCsrf" {
                // attempt to delete the temporary file that was copied to the 
                // "Inbox" directory from importing via email, iMessage, etc.
                try fileManager.removeItem(at: eachURL)
           }
       }
    }
    

    如果有人有更优雅的解决方案,也请回复。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-15
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      相关资源
      最近更新 更多