iosswiftimportuidocumentpickervc

How to use UIDocumentPickerViewController to pick a custom type?


I'm having a problem importing a file from the cloud. I'm dealing with a custom filetype that I didn't actually create. So I don't know what the actual extension type is (for example com.example.pdf). The file ends in .toc, so is there a way I can only accept files that end in .toc?

For example something like this:

UIDocumentPickerViewController(documentTypes: [String("*.toc")], in: .import)

If this is not possible, is there a way to allow ANY file to be selected? From there I could just check the last 4 digits and let the user know that the file must end in .toc?

To try and get more information on what type of file this is, I have also ran the following command in terminal:

file -I filename.toc

Which returned:

filename.toc: text/html; charset=us-ascii

Because this says it's a "text/html" file I tried using:

UIDocumentPickerViewController(documentTypes: [String(kUTTypeText)], in: .import)

However, this didn't allow me to select the file... Does anyone have any ideas?


Solution

  • kUTTypeText is for text/plain. You want kUTTypeHTML for text/html.

    iOS may not agree that "toc" files are text/html. You should probably add an appropriate "Exported UTI" to your Info.plist that defines an appropriate mime-type for the "toc" extension. Then use that mime-type with your UIDocumentPickerViewController.

    See Uniform Type Identifiers Overview for details on setting up an exported UTI in your app.

    If you want to allow any file, use kUTTypeItem.