swiftios16utiuidocumentpickerviewcontrolleruttype

What is an iOS 16-compatible way of turning a list of uniform type indicator strings (e.g., ["public.text", "public.image"]) into a list of UTType?


My app has a list of supported uniform type indicator (UTI) strings:

    public static let supportedUTIs = ["public.text", "public.image", "com.adobe.pdf", "public.comma-separated-values-text", "com.microsoft.word.doc", "org.openxmlformats.wordprocessingml.document", "com.microsoft.excel.xls", "org.openxmlformats.spreadsheetml.sheet", "com.microsoft.powerpoint.ppt", "org.openxmlformats.presentationml.presentation", "com.apple.iwork.pages.pages", "com.apple.iWork.Numbers.numbers", "com.apple.keynote.key"]

While working on moving my app's minimum deployment target up to iOS 16, I noticed that the usage of UIDocumentPickerViewController(documentTypes:in:) has changed to UIDocumentPickerViewController(forOpeningContentTypes:) when planning to use import mode for files. Whereas documentTypes accepted [String], the new forOpeningContentTypes parameter requires [UTType].

I need to keep my list of UTI strings in the app since it is being used elsewhere, but is there a nice way to programmatically convert this list into a new list of UTType that I can pass into my UIDocumentPickerViewController?

I've tried some older solutions from the forums here, but they tend not to be compatible with the minimum deployment target of iOS 16 since anything that uses kUTType is deprecated. I've also seen several examples of small type checks (e.g., this file is an image, this is a video, etc.), but I'm not sure what to do with the more specific file extensions in my supportedUTIs list.

Any insight on how to make this list conversion from String to UTType in iOS 16 would be super helpful.


Solution

  • You just need to import UniformTypeIdentifiers and map your strings into UTType using its initializer:

    let utTypes = supportedUTIs.compactMap(UTType.init)