我正在将文件附加到电子邮件中,我想通过 UIDocumentPickerViewController 来完成。基本上,用户点击图标,他可以选择一个文件。一切正常,用户可以选择一个文件,我可以获取它的 url,然后进行文件传输。最近它停止工作,令人惊讶的是,我没有对代码进行任何更改(是的,这听起来很傻)。我发现我的 didPickDocumentAt 委托没有被调用。总而言之,browseFiles 函数在点击时被调用并且 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)
}
}
}