我正在将文件附加到电子邮件,我想通过UIDocumentPickerViewController来完成。基本上,用户单击该图标即可选择文件。一切正常,用户可以选择一个文件,我可以获取它的URL,然后进行文件传输。最近它停止工作,而且令人惊讶的是,我没有对代码进行任何更改(是的,听起来很傻)。我发现的是没有调用我的didPickDocumentAt委托。总而言之,单击时会调用browserFiles函数,并显示documentBrowser,但选择文件不起作用(未调用didPickDocumentAt委托函数和uploadDataFromUrl)
extension EditViewTableViewController: UIDocumentMenuDelegate, UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
self.uploadDataFromUrl(url: url)
}
public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
dismiss(animated: true, completion: nil)
}
func browseFiles() {
let documentBrowserController = UIDocumentPickerViewController(documentTypes: [kUTTypeImage, kUTTypeCompositeContent] as [String], in: UIDocumentPickerMode.import)
if #available(iOS 11.0, *) {
documentBrowserController.allowsMultipleSelection = true
}
documentBrowserController.delegate = self
self.present(documentBrowserController, animated: false, completion: nil)
}
func uploadDataFromUrl(url: URL) {
if FileManager.default.fileExists(atPath: url.path) {
do {
let data = try Data(contentsOf: url)
self.didUploadedTheFile(path: url.path)
} catch {
print(error.localizedDescription)
}
}
}