Reputation: 1780
I want to select files from Google drive and One drive cloud services for that purpose I am using UIDocumentPickerViewController
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.
Upvotes: 4
Views: 4033
Reputation: 2447
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!
Upvotes: 3
Reputation: 2573
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.
Upvotes: 1