1

I'm trying to open cloud storages app in my document picker view controller. I have applied all the necessary delegates method of document picker view controller. When I hit a button it shows me document picker view but the application icons and their name are not shown in it. It only shows browse option in the picker view. I want to show all the storages in front. My code is this,

@IBAction func cloudBtnTapped(_ sender: Any) {

    let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF),String(kUTTypeZipArchive), String (kUTTypePNG), String (kUTTypeJPEG), String (kUTTypeText),String (kUTTypePlainText)], in: .import)
    importMenu.delegate = self
    importMenu.modalPresentationStyle = .fullScreen
    self.present(importMenu, animated: true, completion: nil)
}
 func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

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

    // Start background thread so that image loading does not make app unresponsive
    DispatchQueue.global(qos: .userInitiated).async {

        let imageData:NSData = NSData(contentsOf: self.cico as URL)!

        // When from background thread, UI needs to be updated on main_queue
        DispatchQueue.main.async {
            let image = UIImage(data: imageData as Data)
            self.image1.image = image
        }
    }

    do {
        let weatherData = try NSData(contentsOf: cico as URL, options: NSData.ReadingOptions())
        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)
    _ = navigationController?.popViewController(animated: true)
}

When hit button it shows document picker view like this,
enter image description here

But I want this . picker view like this,
enter image description here

How can I show this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hamza
  • 231
  • 1
  • 8
  • 22

1 Answers1

3

UIDocumentMenuViewController will automatically shown options like dropbox, google drive is you have installed it on your device, other wise it won't be displayed.

You can also add some custom options to the menu by the following method... addOptionWithTitle:image:order:handler: method.

You can use UIDocumentPickerViewController to get the files from the Files apps or iCloud Drive.

let options = [kUTTypePDF as String, kUTTypeZipArchive  as String, kUTTypePNG as String, kUTTypeJPEG as String, kUTTypeText  as String, kUTTypePlainText as String]

let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: options, in: .import)
documentPicker.delegate = self            
documentPicker.modalPresentationStyle = .fullScreen
self.present(documentPicker, animated: true, completion: nil)


extension YourViewController: UIDocumentPickerDelegate {

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

    //from url you can get selected item
   }
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • i have installed all the apps but still isn't showing . If i add customly how will it open the detail of the option when we hit out custom option. @Mahendra GP – Hamza Feb 16 '18 at 11:03
  • why don't you use `UIDocumentPickerViewController` instead of `UIDocumentMenuViewController`? – Mahendra Feb 16 '18 at 11:06
  • This is my first time with document picker view can u give bit direction where should i change my code? @Mahendra GP – Hamza Feb 16 '18 at 11:12
  • Bro what is this for documentTypes: ["public.text"]? and yes i get a url when i click any item from cloud storage how can i show that in table view? Mahendra GP – Hamza Feb 16 '18 at 11:32
  • you need to download in device's document folder or some where else if you want and from that location you can get and display it in tableview. – Mahendra Feb 16 '18 at 11:37
  • when i click any item it show another alert whether u want to print, save or share this file, when i save it where does it go and how can i get it from the location where i save? @Mahendra GP – Hamza Feb 16 '18 at 11:39
  • in the delegate method `didPickDocumentsAt ` you will get the url of the item. – Mahendra Feb 16 '18 at 11:40
  • how to show that url in table view and how can i give the name of that file? @Mahendra GP – Hamza Feb 16 '18 at 11:49
  • The file will be downloaded on device(most probably in tmp folder of your app) and you will be provided a urls in delegate method `didPickDocumentsAt`, so `urls` is an array of urls that you need to work with and fetch the data using `FileManager` available in swift and get the file in your app. – Mahendra Feb 16 '18 at 11:59
  • You Made my day... Thanks – Gaurav Mar 13 '20 at 06:33