![](/img/trans.png)
[英]UIDocumentMenuViewController / UIDocumentPickerViewController slow to show in view
[英]How to get the source of the files from UIDocumentMenuViewController or UIDocumentPickerViewController?
我已经成功实现了一项功能,该功能使用户可以通过UIDocumentMenuViewController
和UIDocumentPickerViewController
将文件选择到我的应用程序中。
但是我找不到如何检测文件的来源。 有类似Files , Google Drive和DropBox等的来源。
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController)
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
我已经完成了所有委托函数,但看不到任何方法。 任何帮助将不胜感激。
你好,加尔文,
请添加委托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)
}
出于隐私原因,这是不可能的。 您不应该知道用户安装了哪些云供应商。
另外,请不要使用不推荐使用的UIDocumentMenuViewController
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.