【问题标题】:No callback while using UIDocumentPickerViewController使用 UIDocumentPickerViewController 时没有回调
【发布时间】:2018-11-20 10:20:08
【问题描述】:

我正在使用 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] 视图服务确实因错误而终止:错误 域=_UIViewServiceErrorDomain 代码=1“(空)” UserInfo={Terminated=断开方法}

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

感谢期待。

【问题讨论】:

    标签: ios swift4 uidocumentpickervc


    【解决方案1】:

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

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

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

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

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

    此外,以下行就足够了。

    present(documentPicker, animated: true)
    

    【讨论】:

    • func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { } 已在 iOS 11 中弃用。因此使用 documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { } 毫无意义。我也试过设置 allowsMultipleSelection 没有帮助。
    • 那么请参考文档。我给了你我已经使用和正在工作的代码 sn-ps,在发布之前在设备上进行了测试。
    • 另外,如果一个解决方案适合您(因为发布了多个),请选择它作为接受的答案,它可以帮助其他用户。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        • 2015-08-17
        • 1970-01-01
        • 1970-01-01
        • 2021-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多