Swift中的文档选择器视图控制器问题

时间:2018-02-07 06:53:27

标签: ios uidocumentpickerviewcontroller

我的视图控制器中有一个按钮,当单击按钮打开文档选择器视图控制器时,我设置了类似KUTypePDF and KUTypeZipArchive的类型,当我从谷歌驱动器中选择任何这些项目时,选择控制器会被解雇并且在控制台中它显示了一个长期未完成的数字字符串,并且不会停止。我很困惑为什么它会显示长串。我想要的是,在我从谷歌驱动器中选择任何文件后,它应该显示在我的应用程序中。我正确获取文件网址。我的代码就是这个,

@IBAction func docsBtnTapped(_ sender: Any) {

    let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF),String(kUTTypeZipArchive)], in: .import)
    importMenu.delegate = self
    importMenu.modalPresentationStyle = .fullScreen
    self.present(importMenu, animated: true, completion: nil)}

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


    let cico = url as URL
    print("The Url is : /(cico)", cico)


    do {
        let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions())
        print(weatherData)
        let activityItems = [weatherData]
        let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        if UI_USER_INTERFACE_IDIOM() == .phone {
            self.present (activityController, animated: true, completion: {
                print("Hello")
            })
        }
        else {
            let popup = UIPopoverController(contentViewController: activityController)
            popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true)
        }

    } catch {
        print(error)
    }


    //optional, case PDF -> render
    //displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest)




}

func documentMenu(_ documentMenu:     UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

    documentPicker.delegate = self
    present(documentPicker, animated: true, completion: nil)

}



func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

    print(" cancelled by user")

    dismiss(animated: true, completion: nil)


}

当我选择PDF或Zip文件的任何项目时,这个长字符串进入控制台, enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用 UIDocumentInteractionController 从网址打开任何类型的预览:

以下是打开网址的简单代码:

var documentController:UIDocumentInteractionController!


@IBAction func btnOpenClicked(_ sender: Any) {
    if let fileURL = Bundle.main.url(forResource: "icon", withExtension: "jpg") {
        // Instantiate the interaction controller
        self.documentController = UIDocumentInteractionController.init(url: fileURL)
        self.documentController.delegate = self
        self.documentController.presentPreview(animated: true)
    }
    else {
        print("File missing! Button has been disabled")
    }
}

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    return self
}