首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在swift中使用UIDocumentPickerViewController(documentTypes:)

在swift中使用UIDocumentPickerViewController(documentTypes:)
EN

Stack Overflow用户
提问于 2021-02-02 13:11:05
回答 1查看 753关注 0票数 0

在我的代码中使用UIDocumentPickerViewConroller选择应用程序中的音频文件时,出现此错误,并且我找不到(documentTypes: )at UIDocumentPickerViewController

代码语言:javascript
复制
 @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]){
        
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-02 13:42:24

UIDocumentPickerViewController(documentTypes: String,in: UIDocumentPickerMode)在iOS 14.0中已弃用

该方法被替换为UIDocumentPickerViewController(forOpeningContentTypes contentTypes: UTType,asCopy: Bool)方法

首先,您需要导入UniformTypeIdentifiers

代码语言:javascript
复制
import UniformTypeIdentifiers

因此,您可以按如下方式使用它

代码语言:javascript
复制
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)
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66003954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档