How to get the source of the files from UIDocumentMenuViewController or UIDocumentPickerViewController?

717 Views Asked by At

I have successfully implemented a feature letting the user pick a file to my app through UIDocumentMenuViewController and UIDocumentPickerViewController.

But I cannot find out how can I detect the source of the file. There are sources like Files, Google Drive and DropBox etc.

func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController)
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)

I have gone through all the delegate functions and do not see any ways. Any help would be greatly appreciated.

2

There are 2 best solutions below

0
On

Hello Calvin,

Please add delegate UIDocumentMenuDelegate,UIDocumentPickerDelegate

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
            let docurl = url as URL
            print("The Url is : \(docurl)")
            let urlRequest = NSURLRequest(url: docurl)
            do {
                let fileAttributes = try FileManager.default.attributesOfItem(atPath: url.path)
                    let theData = try NSURLConnection.sendSynchronousRequest(urlRequest as URLRequest, returning: nil)
                    let fileExtension = urlRequest.url?.lastPathComponent
                    var strFileNAME = ""
                    if (fileExtension?.contains("pdf"))! {
                        print("pdf")
                    } else {
                        print("doc")
                    } 
                    var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last as NSURL?
                    docURL = docURL?.appendingPathComponent("\(String(describing: fileExtension))") as NSURL?
                    try theData.write(to: docURL! as URL)
                    self.DocumentData = theData
                    print("downloaded")
                }
            } catch (let writeError) {
                print("error : \(writeError)")
            }
            let docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last)
            do {
                let contents = try (FileManager.default.contentsOfDirectory(at: docURL!, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles))
                print(contents)
            }
            catch (let error) {
                print("error contents \(error)")
            }
        }

        public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
            documentPicker.delegate = self
            present(documentPicker, animated: true, completion: nil)
        }

        func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
            dismiss(animated: true, completion: nil)
        }
0
On

This is not possible for privacy reasons. You should not be able to tell which cloud vendors users have installed.

Also, please do not use UIDocumentMenuViewController, which is deprecated.