【问题标题】:How can I create an app icon folder in the files app and copy items to it?如何在文件应用程序中创建应用程序图标文件夹并将项目复制到其中?
【发布时间】:2021-01-19 16:29:16
【问题描述】:

我想将一些文件(声音、文本等)从 iOS 应用程序传输到文件应用程序。此外,我想将所有这些项目放入与我的应用程序同名的文件夹中 - 例如 GarageBand 或 KeyNote。

在 Xcode 中,我确实启用了 iCloud 文档功能 - 我还定义了一个容器“iCloud.xxx.yyy” - 请参见下面的代码。

guard let fileURL = Bundle.main.url(forResource: "test", withExtension: "aiff") else { return }

guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: "iCloud.xxx.yyy") else { return }

if !FileManager.default.fileExists(atPath: containerURL.path) {
    try FileManager.default.createDirectory(at: containerURL, withIntermediateDirectories: true, attributes: nil)
}

let backupFileURL = containerURL.appendingPathComponent("test.aiff")
if FileManager.default.fileExists(atPath: backupFileURL.path) {
    try FileManager.default.removeItem(at: backupFileURL)
    try FileManager.default.copyItem(at: fileURL, to: backupFileURL)
} else {
    try FileManager.default.copyItem(at: fileURL, to: backupFileURL)
}

当我运行我的代码时,它似乎可以工作 - 无论如何,我看不到代表我的应用程序名称的文件夹,也看不到文件应用程序中的“test.aiff”文件。我的方法有什么问题?

【问题讨论】:

  • 文件应用程序 = Finder?记录 containerURL,然后按 command + shift + G 在 Finder 中导航到它。它在那里吗?日志甚至显示吗?
  • 不,我的意思是文件应用程序中的 iCloud Drive
  • 好的,现在您使用的是 iOS。您可能应该添加您的问题。
  • 好的,我会改正的

标签: ios swift icloud file-transfer ubiquitycontainer


【解决方案1】:

您无需复制/移动任何文件。您需要的是允许从其他应用程序访问您的应用程序文档。只需转到您的信息列表并允许“支持文档浏览器”。您的 Documents 目录中的所有文档都将在那里自动可用。

【讨论】:

  • 感谢您的提示!我确实纠正了 info.plist 文件 - 无论如何,我的应用程序文件夹中没有任何项目出现在文件应用程序中。也许转移需要一些时间?
  • AFAIR 您需要定义要在您的应用程序中支持的文档类型。
  • 尝试注销 iCloud 并重新登录。你是在真机还是模拟器上测试?
  • 都在模拟器上,都在 iPhone 上
  • 谢谢里奥!现在我确实重置了模拟器上的所有内容并登录到 iCloud。现在我可以在“我的 iPhone”下找到我的 app 文件夹,其中包含一些文件,例如声音和 sqlite 文件。我不确定它们是否也会出现在 iCloud Drive 中——也许需要一些时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 2016-08-01
  • 2010-10-19
相关资源
最近更新 更多