在我的代码中使用 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]){
}
}
Run Code Online (Sandbox Code Playgroud)
UIDocumentPickerViewController(documentTypes: [String], in: UIDocumentPickerMode) 在 iOS 14.0 中被弃用
该方法被这个 UIDocumentPickerViewController(forOpeningContentTypes contentTypes: [UTType], asCopy: Bool) 方法取代
首先,您需要导入 UniformTypeIdentifiers
import UniformTypeIdentifiers
Run Code Online (Sandbox Code Playgroud)
所以,你可以使用它如下
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
327 次 |
| 最近记录: |