使用UIDocumentPickerViewController时没有回调



我正在使用UIDocumentPickerViewController从iPhone中选择一个文档。

我以以下方式实现了该功能。

class Si3EntityDocumentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIDocumentInteractionControllerDelegate, UIDocumentPickerDelegate, UINavigationControllerDelegate, UIDocumentMenuDelegate {
override func viewDidLoad() {
super.viewDidLoad()
setLoader()
getDocuments(id:entity.id)
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .red
button.setTitle("Upload Doc", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
// Do any additional setup after loading the view.
}
@objc func buttonAction(sender: UIButton!){
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypeText),String(kUTTypeContent),String(kUTTypeItem),String(kUTTypeData)], in: .import)
documentPicker.delegate = self
present(documentPicker, animated: true, completion: {
documentPicker.delegate = self
} )
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print(urls)
}
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt url: URL)方法在iOS 11中已被弃用,因此我已将其替换为func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])

由于某种原因,我没有在func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])中得到回调

我在这个类中也有一些其他代码,但我没有包括它。当我选择一个文档或单击取消时,我在控制台中收到以下错误

[DocumentManager]视图服务确实终止,并出现错误:error域=_UIViewServiceError域代码=1"(null("UserInfo={Terminated=disconnect method}

我知道我错过了一些非常愚蠢的事情,任何帮助都将不胜感激。

期待中的感谢。

您可以查看Apple文档以更好地理解。

UIDocumentPickerViewController的默认行为是选择一个文档,您必须使用:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
}

如果使用UIDocumentPickerViewController拾取多个文档,则必须将allowsMultipleSelection属性设置为true并实现

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
}

此外,下面这行就足够了。

present(documentPicker, animated: true)

我在swift 3 中使用了这种方法

func openImportDocumentPicker() {
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true, completion: { _ in })
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if controller.documentPickerMode == .import {
let alertMessage: String = "Successfully imported (url.absoluteURL)"
}
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Cancelled")
}

在设备上试用。当我登录iCloud并试图查看目录时,模拟器对我不起作用。

相关内容

  • 没有找到相关文章

最新更新