快速使用UIDocumentPickerViewController(documentTypes:)
在我的代码中使用 UIDocumentPickerViewConroller 在应用程序中选择音频文件时,出现此错误并且我找不到(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]){
}
}
回答
UIDocumentPickerViewController(documentTypes: [String], in: UIDocumentPickerMode) 在 iOS 14.0 中被弃用
该方法被这个 UIDocumentPickerViewController(forOpeningContentTypes contentTypes: [UTType], asCopy: Bool) 方法取代
首先,您需要导入 UniformTypeIdentifiers
import UniformTypeIdentifiers
所以,你可以使用它如下
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)
THE END
二维码