Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialization of UIDocumentPickerViewController in iOS 14

I have a UIDocumentPickerViewController that picks only images. This works fine in iOS 13:

let supportedTypes: [String] = ["public.image"] let documentPickerViewController = UIDocumentPickerViewController(documentTypes: supportedTypes, in: .import) 

I'm trying to achieve the same in iOS 14.

However, init(documentTypes:in:) was deprecated in iOS 14.0.

So I'm assuming I have to use this initializer instead: init(forOpeningContentTypes:asCopy:)

The forOpeningContentTypes argument expects an array of UTType.
I'm not sure how to use / import it.
Is it like [UTType.Image]?

let supportedTypes: [UTType] = [UTType.Image] self.viewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true) 

👆🏻 This won't even compile because Cannot find type 'UTType' in scope.
I also tried import MobileCoreServices and even import CoreServices, but that didn't help.

The documentation for the new Document Picker initializer points to this UTTypeReference -- so I tried using that, but it can't be found either.

How can I have a UIDocumentPickerViewController that picks only images in iOS 14?

Update

This works (thanks @Warren):

import UniformTypeIdentifiers let supportedTypes: [UTType] = [UTType.image] let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true) 
like image 833
backslash-f Avatar asked Jun 30 '20 08:06

backslash-f


Video Answer


1 Answers

Add import UniformTypeIdentifiers to your swift file to get access to UTType

See the documentation for more usages.

And the WWDC2020 video "Build document-based apps in SwiftUI" for a practical demo.

like image 183
Warren Burton Avatar answered Sep 24 '22 17:09

Warren Burton