我正在使用UIDocumentPickerViewController从“文件”中选择文档并将其上传到服务器。我能够成功访问文件,但是单击文件后,不会调用委托方法。
我已经使用以下代码调用文档选择器:
class Uploads: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func uploadDocument(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypePlainText)], in: .import)
documentPicker.delegate = self
if #available(iOS 11.0, *) {
documentPicker.allowsMultipleSelection = false
} else {
}
present(documentPicker, animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
extension Uploads: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print(urls.first)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Cancelled")
}
}
我注意到调用委托方法时收到以下警告:
实例方法'documentPicker(:didPickDocumentsAt :)'几乎匹配 的可选要求'documentPicker(:didPickDocumentsAt :) 协议'UIDocumentPickerDelegate'
将'documentPicker(_:didPickDocumentsAt :)'设为私有可对此静音 警告
尽管我无法弄清楚为什么收到此警告,但我相信由于此警告而未调用委托方法。
答案 0 :(得分:0)
如果将采用“ UIDocumentPickerDelegate”协议的类声明为“打开”,则会出现问题。
例如,此类将存在问题:
open class FilePickerHelper: UIDocumentPickerDelegate
该课程将不会出现问题:
class FilePickerHelper: UIDocumentPickerDelegate
答案 1 :(得分:0)
希望共享代码示例会有所帮助:
}