2

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

1
  • "long un finished strings" is because of print(weatherData) statement you have used. Commented Feb 7, 2018 at 7:50

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

10 Comments

i want to open the path that i'm getting in console. @Nirmalsinh
You can give your if let fileURL = Bundle.main.url(forResource: "icon", withExtension: "jpg") here
let me try. @Nirmalsinh
It is for preview bro, i want to get that file in my app so that i could send it further to the server. @Nirmalsinh
Then you need to download file into local cache memory then you can upload to your server.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.