4

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.

Error Screenshot

 @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]){
        
    }
}
1
  • 1
    Have you imported import MobileCoreServices framework? Commented Feb 2, 2021 at 5:20

1 Answer 1

17

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)
Sign up to request clarification or add additional context in comments.

3 Comments

I tried but I can't import UniformTypeIdentifiers. should I update Xcode?
I’m downloading 12.5 now
I've Xcode Version 12.2 and it is working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.