4

I want to select files from Google drive and One drive cloud services for that purpose I am using UIDocumentPickerViewController

enter image description here

ex:

func didPressOneDriveChoose() {
    let importMenu = UIDocumentPickerViewController(documentTypes: [(kUTTypePDF as String), (kUTTypeJPEG as String), (kUTTypePNG as String)], in: .import)
    importMenu.delegate = self
    importMenu.modalPresentationStyle = .formSheet
    self.show(importMenu, sender: nil)
}

Recived callBack on delegate method:

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

    var arrFiles = [Dictionary<String, AnyObject>]()
    for url in urls {
        // returns local document directory URL
        // needs original GoogleDrive or OneDrive URL
    }
}

but I want to know is there any way to get which cloud service is selected by the user and also I want original URL pointing to that URL? Currently, it returns local documents directory URL after downloading that file on click.

Kara
  • 6,115
  • 16
  • 50
  • 57
iamVishal16
  • 1,780
  • 18
  • 40

2 Answers2

3

You initialize as so:

let importMenu = UIDocumentPickerViewController(documentTypes: [(kUTTypePDF as String), (kUTTypeJPEG as String), (kUTTypePNG as String)], in: .import)

If you read the documentation on this particular picker mode .import it states:

The URLs refer to a copy of the selected documents. These documents are temporary files. They remain available only until your application terminates. To keep a permanent copy, move these files to a permanent location inside your sandbox.

.open seems like it would fit your use case better.

The URLs refer to the selected documents. The provided URLs are security-scoped, referring to files outside your app’s sandbox. For more about working with external, security-scoped URLs, see Requirements.

But seeing as you are merely observing the url's to identify the service and not actually trying to manually access those documents, you shouldn't need to worry about breaking any requirements. So change the UIDocumentPickerViewController initialization to:

let importMenu = UIDocumentPickerViewController(documentTypes: [(kUTTypePDF as String), (kUTTypeJPEG as String), (kUTTypePNG as String)], in: .open)

And it should work!

NSGangster
  • 2,397
  • 12
  • 22
1

You won't be able to know the cloud service (unless you start inferring it from the URL... bad idea) for privacy reasons. However, you can get the files from their original location, not copies, if you use the open mode instead of the import mode (in: .open). Warning, if you use the open mode, you start opening documents in place, so you need to use File Coordination appropriately.

Thomas Deniau
  • 2,488
  • 1
  • 15
  • 15
  • unless you start inferring it from the URL... bad idea. Can you please explain it why It's a bad idea? – iamVishal16 Apr 12 '18 at 05:44
  • Because this is a private implementation detail and the URLs can change in future versions of iOS. Actually, I have just checked and it looks like the only thing you can get from the URL is a UUID, so it’s probably impossible already. – Thomas Deniau Apr 13 '18 at 07:41
  • Thanks, It will help a lot. – iamVishal16 Apr 16 '18 at 04:44