在 iOS 上通过扩展名获取文件类型

Getting the file kind by extension on iOS

当我获取 .dae 文件并在 OSX 的查找器上执行 File, Info 时,我看到

Kind: Digital Asset Exchange (DAE)

.rtfd 显示

Kind: Rich Text Document with Attachments

但这是OSX。现在,如何通过文件的 NSURL 发现 iOS 上的文件类型?

可能还有其他方法,但一种方法是使用 MobileCoreServices 提供的统一类型标识符 (UTI) 服务。

import MobileCoreServices

只需获取与扩展的 UTI 关联的描述:

if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, nil)?.takeRetainedValue(), let description = UTTypeCopyDescription(uti)?.takeRetainedValue() {
    print(description)
}

请参阅 文件系统编程指南中的 How the System Identifies the Type of Content in a File