如何从UIDocumentMenuViewController或UIDocumentPickerViewController获取文件的来源?

时间:2018-05-31 10:29:36

标签: ios swift uidocumentpickerviewcontroller uidocumentmenuviewcontroller

我已成功实施了一项功能,允许用户通过UIDocumentMenuViewControllerUIDocumentPickerViewController为我的应用选择文件。

但我无法找到如何检测文件的来源。有文件 Google云端硬盘 DropBox 等来源。

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

我已经完成了所有委托功能,但没有看到任何方法。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

Hello Calvin,

请添加代理人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)
        }

答案 1 :(得分:0)

出于隐私原因,这是不可能的。您不应该知道哪些云供应商用户已安装。

另外,请不要使用已弃用的UIDocumentMenuViewController