Hamza
Hamza

Reputation: 251

Document Picker View Controller issue in Swift

I have a button in my view controller, when click a button it open a document picker view controller, i have set type like KUTypePDF and KUTypeZipArchive , when i select any of these item from google drive the picker controller gets dismiss and in console it shows a long un finished strings of number that doesn't stop. I'm confused why it is showing that long string. What i want is that after i select any file from google drive it should be shown in my app. I'm getting the file url right. My code is this,

@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)


}

This long string come in console when i select any item of PDF or Zip file, enter image description here

Upvotes: 2

Views: 3306

Answers (1)

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

You can use UIDocumentInteractionController to open any kind preview from url:

Here is Simple code to open 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
}

Upvotes: 2

Related Questions