SpaceDog SpaceDog - 6 months ago 15
iOS Question

Getting the file kind by extension on iOS

When I take a

.dae
file and do
File, Info
on OSX's finder I see

Kind: Digital Asset Exchange (DAE)


.rtfd
shows

Kind: Rich Text Document with Attachments


But this is OSX. Now, how do I discover the kind of file on iOS by the NSURL of that file?

Rob Rob
Answer

There may be other ways, but one way is to use the uniform type identifier (UTI) services provided by MobileCoreServices.

import MobileCoreServices

Just get the description associated with the extension's UTI:

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

See the How the System Identifies the Type of Content in a File in the File System Programming Guide.