我已成功实施了一项功能,允许用户通过UIDocumentMenuViewController
和UIDocumentPickerViewController
为我的应用选择文件。
但我无法找到如何检测文件的来源。有文件, Google云端硬盘和 DropBox 等来源。
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController)
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
我已经完成了所有委托功能,但没有看到任何方法。任何帮助将不胜感激。
答案 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
。