从UIDocumentPickerViewController显示文件夹名称

时间:2020-07-13 09:45:40

标签: ios swift

我正在使用UIDocumentPickerViewController来允许用户选择要在其中保存文本文件的文件夹。除了能够显示确认已保存文件位置的消息以外,其他所有方法都有效。

屏幕截图显示了UIDocumentPickerViewController显示的典型文件夹列表,用户可以选择:

UIDocumentPickerViewController

显示给用户的文件夹名称是预期的名称。但是,UIDocumentPickerViewController返回的URL与显示的URL不同,因此不适合在确认消息中使用:

.../data/Library/Mobile Documents/com~apple~CloudDocs
.../data/Library/Mobile Documents/com~apple~CloudDocs/Desktop
.../data/Library/Mobile Documents/com~apple~CloudDocs/Documents
.../data/Library/Mobile Documents/3L68KQB4HG~com~readdle~CommonDocuments/Documents
.../data/Library/Mobile Documents/com~apple~CloudDocs/Downloads
.../data/Library/Mobile Documents/iCloud~net~jermware~gjLists/Documents
.../data/Library/Mobile Documents/iCloud~is~workflow~my~workflows/Documents
.../data/Containers/Shared/AppGroup/EB502BF4-9272-466F-9F5D-0CA97E35E687/File Provider Storage

在某些网址中,我可以仅使用最后一个路径部分,例如台式机文档下载,但是对于某些情况,它是最后一个路径组件中的第二个,例如 gjLists ,而其他路径则完全不同,例如 Readdle文档快捷方式在我的iPhone上

这使得很难/不可能可靠地解析URL以提取UIDocumentPickerViewController显示给用户的文件夹。

经过一番调查,我发现FileManager.default.displayName API可以在指定路径返回文件或目录的显示名称。

这会有所改善,为上述URL提供以下文件夹名称:

iCloud Drive
iCloud Drive
iCloud Drive
Documents by Readdle
Downloads
gjLists
Shortcuts
File Provider Storage

我现在为 Readdle文档下载 gjLists 快捷方式有了正确的名称,但是台式机文档 iCloud Drive “在我的手机上”仍为“文件提供商存储”

在给定UIDocumentPickerViewController返回的URL的情况下,有人知道如何获得选择器中显示的文件夹名称吗?向用户显示一条确认消息,表明文件已成功保存到“文件提供者存储” 文件夹,而不是“在我的iPhone上”

0 个答案:

没有答案