2

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

@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 文件项目时,这个长字符串会出现在控制台中, 在此处输入图像描述

4

1 回答 1

2

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

这是打开网址的简单代码:

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
}
于 2018-02-07T08:22:59.567 回答