我使用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视图服务终止时出现了错误: Domain=_UIViewServiceErrorDomain Code=1 "(null)“UserInfo={Terminated=disconnect方法}
我知道我错过了一些很愚蠢的东西,任何帮助都是值得感激的。
感谢你的期待。
发布于 2018-11-20 10:57:53
您可以查看苹果文档以获得更好的理解。
UIDocumentPickerViewController的默认行为是选择一个文档,您必须使用:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
}如果使用allowsMultipleSelection选择多个文档,则必须将UIDocumentPickerViewController属性设置为true并实现
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
}另外,下面一行就足够了。
present(documentPicker, animated: true)发布于 2019-09-21 10:47:49
试试看一个装置。当我登录到iCloud并试图查看目录时,模拟器没有为我工作。
https://stackoverflow.com/questions/53390825
复制相似问题