Reputation: 41
While using UIDocumentPickerViewConroller in my code to select an audio file in app, this error came out and I can't find (documentTypes: )at UIDocumentPickerViewController
.
@IBAction func AddMusic(_ sender: UIButton) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeAudio as String], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true, completion: nil)
}
}
extension ViewController: UIDocumentPickerDelegate{
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]){
}
}
Upvotes: 4
Views: 10691
Reputation: 7193
UIDocumentPickerViewController(documentTypes: [String], in: UIDocumentPickerMode) was deprecated in iOS 14.0
That Method is replaced by this UIDocumentPickerViewController(forOpeningContentTypes contentTypes: [UTType], asCopy: Bool) method
First, You need to import UniformTypeIdentifiers
import UniformTypeIdentifiers
So, You can use this as below
let supportedTypes: [UTType] = [UTType.audio]
let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
pickerViewController.delegate = self
pickerViewController.allowsMultipleSelection = false
pickerViewController.shouldShowFileExtensions = true
self.present(pickerViewController, animated: true, completion: nil)
Upvotes: 17