我无法获得 UIDocumentPickerMode。开工。
UIDocumentPicker 模式。导入有效,但生成的 url 用于临时文件(用于模拟器)“file:///Users/ name /Library/Developer/CoreSimulator/Devices/ identifier /data/Containers/Data/Application/ identifier / tmp / myApp /filename”而不是“file:///Users/ name /Library/Developer/CoreSimulator/Devices/ identifier /data/Containers/Data/Application/ identifier / Documents / myAppDirectory /filename”
我无法知道该文件是否真的位于我有权访问的子目录或其他应用程序目录中。
如果我不能为此目的使用文档选择器,我应该使用什么?
如果我应该使用临时文件,我还没有在 Swift 4 中找到 startAccessingSecurityScopedResource 的工作示例。
附录-这是我尝试过的代码:
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if controller.documentPickerMode == UIDocumentPickerMode.import {
// This gives me a path to the temporary file
let filePath = url.path
}
if (controller.documentPickerMode == UIDocumentPickerMode.open)
{
// this returns with nil for url
let isAccessable:Bool = url.startAccessingSecurityScopedResource()
if(!isAccessable)
{
return;
}
else
{
let filePath = url.path
}
// Do work inside
url.stopAccessingSecurityScopedResource()
}
}
// elsewhere in the code
// open a document picker, select a file
// let importFileMenu = UIDocumentPickerViewController(documentTypes: ["public.data"], in: UIDocumentPickerMode.import)
let importFileMenu = UIDocumentPickerViewController(documentTypes: ["public.data"], in: UIDocumentPickerMode.open)
importFileMenu.delegate = self
importFileMenu.modalPresentationStyle = .formSheet
self.present(importFileMenu, animated: true, completion: nil)
我究竟做错了什么?