2

我已经成功实现了一项功能,让用户通过UIDocumentMenuViewController和为我的应用选择文件UIDocumentPickerViewController

但我不知道如何检测文件的来源。有FilesGoogle DriveDropBox等来源。

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

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

4

2 回答 2

0

你好卡尔文,

请添加代表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)
        }
于 2018-05-31T12:25:41.710 回答
0

出于隐私原因,这是不可能的。您应该无法分辨用户安装了哪些云供应商。

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

于 2018-06-01T06:26:34.477 回答