我正在使用 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 Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}
我知道我错过了一些非常愚蠢的东西,感谢您的帮助。
感谢期待。