如何获取UIDocumentPickerViewController返回的URL的显示文件夹名称和App图标?

时间:2020-04-18 17:57:35

标签: ios swift url icloud uidocumentpickerviewcontroller

如何获取UIDocumentPickerViewController返回的URL的显示文件夹名称和应用程序图标?

这是我的Swift游乐场代码和下面的一些调试打印输出的示例:

if let newUrl = urlFromPicker {

    // 1 
    print("[DEBUG] newUrl: [\(newUrl)]")

    // 2 
    let res = try! newUrl.promisedItemResourceValues(forKeys: [.ubiquitousItemContainerDisplayNameKey])
    if let displayName1 = res.ubiquitousItemContainerDisplayName {
        print("[DEBUG] newUrl displayName1: [\(displayName1)]")
    }

    // 3 
    if let displayName2 = FileManager.default.displayName(atPath: newUrl.absoluteString).removingPercentEncoding {
        print("[DEBUG] newUrl displayName2: [\(displayName2)]")
    }
}

案例1 :从iCloud Drive(对于本示例PDF Viewer)打开某个应用程序的文档文件夹:

[DEBUG] newUrl: [file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~pspdfkit~viewer/Documents/]
[DEBUG] newUrl displayName1: [PDF Viewer]
[DEBUG] newUrl displayName2: [Documents]

案例2 :从Dir打开同一文档目录的子文件夹iCloud Drive

[DEBUG] newUrl: [file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~pspdfkit~viewer/Documents/Dir/]
[DEBUG] newUrl displayName1: [PDF Viewer]
[DEBUG] newUrl displayName2: [Dir]

由于我在PDF Viewer的设备上也很少使用同一On My iPhone应用程序的文档, 这是本地文档的两种相同情况(目录/子目录):

案例3 :从PDF Viewer打开On My iPhone的本地文档文件夹:

[DEBUG] newUrl: [file:///private/var/mobile/Containers/Data/Application/XXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBBBB/Documents/]
[DEBUG] newUrl displayName2: [Documents]

案例4 :本地子文件夹:

[DEBUG] newUrl: [file:///private/var/mobile/Containers/Data/Application/XXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBBBB/Documents/Subdir/]
[DEBUG] newUrl displayName2: [Subdir]

问题

  • 因为有可能看到URL的方法promisedItemResourceValues(forKeys:).ubiquitousItemContainerDisplayNameKey不适用于本地文件。
  • 如何获取本地文件正在使用Documents文件夹的应用程序的名称(与iCloud情况下displayName1输出的结果相同)
  • 是否可以获取与显示UIDocumentPickerViewController相同的应用图标?

PS 我知道,通过将私有API用作LSApplicationWorkspace,我可以使用提取的应用程序捆绑包ID(来自URL的XXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBBBB)来获取应用程序的名称和图标,但需要一种公开的方式才能将我的应用提交到AppStore。

在此先感谢您的关注和帮助。

0 个答案:

没有答案