使用UIDocumentPickerViewController时没有回调

时间:2018-11-20 10:20:09

标签: ios swift4 uidocumentpickervc

我正在使用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代码= 1“(空)”   UserInfo = {Terminated = disconnect方法}

我知道我错过了一些非常愚蠢的东西,感谢您的帮助。

感谢您的期待。

3 个答案:

答案 0 :(得分:0)

您可以查看Apple documentation以获得更好的理解。

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

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

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

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

此外,以下行就足够了。

present(documentPicker, animated: true)

答案 1 :(得分:0)

我迅速使用了这种方法

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")
}

答案 2 :(得分:0)

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