- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
documentBrowser(_:didRequestDocumentCreationWithHandler:)
的文档说,“创建一个新文档并将其保存到临时位置。如果您使用 UIDocument
子类来创建文档,您必须在调用 importHandler
block 之前将其关闭。”
因此,我通过获取用户临时目录 (FileManager.default.temporaryDirectory
) 的 URL 并附加名称和扩展名(获取类似于“file:///private”的路径)来创建文件 URL/var/mobile/Containers/Data/Application/C1DE454D-EA1E-4166-B137-5B43185169D8/tmp/Untitled.uti”)。但是当我调用 save(to:for:completionHandler:)
传递这个 URL 时,完成处理程序永远不会被回调。我还尝试使用 url(for:in:appropriateFor:create:)
在用户的临时目录中传递一个子目录——完成处理程序仍然没有被调用。
我理解文档浏览器 View Controller 是由一个单独的进程管理的,它有自己的读/写权限。除此之外,我很难理解问题出在哪里。新文档可以临时保存在哪里,以便文档浏览器进程可以移动它们?
更新:截至目前的测试版,我现在看到域 NSFileProviderInternalErrorDomain
和代码 1
的错误被记录:“不允许读者访问 URL。 “至少这是对正在发生的事情的确认……
最佳答案
因此,首先,如果您使用的是自定义 UTI,则必须正确设置它。我的看起来像这样……
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string>icon-file-name</string> // Can be excluded, but keep the array
</array>
<key>CFBundleTypeName</key>
<string>Your Document Name</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.custom-uti</string>
</array>
</dict>
</array>
和
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string> // My doc is saved as Data, not a file wrapper
</array>
<key>UTTypeDescription</key>
<string>Your Document Name</string>
<key>UTTypeIdentifier</key>
<string>com.custom-uti</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>doc-extension</string>
</array>
</dict>
</dict>
</array>
还有
<key>UISupportsDocumentBrowser</key>
<true/>
我将 UIDocument
子类化为 MyDocument
并添加以下方法来创建新的临时文档...
static func create(completion: @escaping Result<MyDocument> -> Void) throws {
let targetURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Untitled").appendingPathExtension("doc-extension")
coordinationQueue.async {
let document = MyDocument(fileURL: targetURL)
var error: NSError? = nil
NSFileCoordinator(filePresenter: nil).coordinate(writingItemAt: targetURL, error: &error) { url in
document.save(to: url, for: .forCreating) { success in
DispatchQueue.main.async {
if success {
completion(.success(document))
} else {
completion(.failure(MyDocumentError.unableToSaveDocument))
}
}
}
}
if let error = error {
DispatchQueue.main.async {
completion(.failure(error))
}
}
}
}
然后如下初始化和显示DBVC:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
lazy var documentBrowser: UIDocumentBrowserViewController = {
let utiDecs = Bundle.main.object(forInfoDictionaryKey: kUTExportedTypeDeclarationsKey as String) as! [[String: Any]]
let uti = utiDecs.first?[kUTTypeIdentifierKey as String] as! String
let dbvc = UIDocumentBrowserViewController(forOpeningFilesWithContentTypes:[uti])
dbvc.delegate = self
return dbvc
}()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = documentBrowser
window?.makeKeyAndVisible()
return true
}
}
我的委托(delegate)方法如下:
func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Swift.Void) {
do {
try MyDocument.create() { result in
switch result {
case let .success(document):
// .move as I'm moving a temp file, if you're using a template
// this will be .copy
importHandler(document.fileURL, .move)
case let .failure(error):
// Show error
importHandler(nil, .none)
}
}
} catch {
// Show error
importHandler(nil, .none)
}
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
let document = MyDocument(fileURL: destinationURL)
document.open { success in
if success {
// Modally present DocumentViewContoller for document
} else {
// Show error
}
}
}
差不多就这些了。让我知道你过得怎么样!
关于ios - 使用 UIDocumentBrowserViewController 创建文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45341934/
我正在使用 UIDocumentBrowserViewController 构建应用程序。一切正常(位置和文档出现,并且可以创建,可选择) 突然,文档浏览器显示为空。没有地点,没有文件…… 今天之前,
当我使用 UIDocumentBrowserViewController 并获取文件的 documentURL 时,例如“file:///private/var/mobile/Containers/D
我正在开发一个新的应用程序,我希望用户能够从 iCloud 或外部驱动器中选择一个文件夹。如何允许他们使用 UIDocumentBrowserViewController 选择文件夹? 我尝试将 al
我正在使用 UIDocumentBrowserViewController 来存储文档 (iOS 11+) 的“新方式”。无需 iCloud API、权利等——即可正常工作(在单独的文档浏览器进程的控
有什么方法可以禁止在我的基于 UIDocumentBrowserViewController 且还提供 FileProvider 的应用程序中导出文件? 用户只能在应用中查看和编辑文件。 用户应该能够
Apple 的文档很少涉及基于 UIDocumentBrowserViewController 的应用程序,这些应用程序希望支持同时打开多个文档。 我想启用它,以便用户可以轻松地在两个或多个文档之间复
我在 swift 中遇到了 DocumentBrowserViewController 的一些有趣问题,我想停止多个文件选择并隐藏导航栏中的 Salect 按钮。 出于这个原因,我使用了属性 allo
documentBrowser(_:didRequestDocumentCreationWithHandler:) 的文档说,“创建一个新文档并将其保存到临时位置。如果您使用 UIDocument 子
我一直在开发一个新的基于文档的应用程序,并且对新的 UIDocumentBrowserController 感到非常高兴...尝试为文档浏览器 UI 推出我自己的解决方案很棘手! 创建文档后,我在让浏
UIDocumentBrowserViewController 与 UIDocumentPickerViewController 之间有什么区别,目前使用 UIDocumentPickerViewCo
我有一个现有的基于 Objective-C 文档的应用程序,我用新的 UIDocumentBrowserViewController 替换了我以前的文件管理器,一切正常——除了我完全不知道如何使用模板
Apple 表示,为了在"file"应用程序中显示您应用程序的文档,您的 Info.plist 一方面必须打开“支持文档浏览器”,或者“应用程序支持 iTunes 文件共享”另一方面,“支持就地打开文
事实如下:我有一个使用 iOS 11 的 UIDocumentBrowserViewController 的基于文档的应用程序。正如文档中所述,我将其设置为我的 Root View Controlle
我需要检查目录中的文件是否存在并且可以打开,但是我的 FileManager.default.fileExists(atPath: )返回假。我无法获取所选目录中的项目列表,无法检查我的项目是否存在。
我使用 UIDocumentViewController(来自 Xcode 9 模板)为 iOS 11 创建了一个应用程序。我注册了文档类型以支持 .swift 文件(public.swift-scr
我正在尝试将自定义事件添加到我的 UIDocumentBrowserController 长按菜单 - UIActivityViewController。选择默认共享事件后,它会很好地指向用户刚刚长按
在 iOS 13 上测试时,UIDocumentBrowserViewController 遇到了一个非常奇怪的问题。importHandler(newDocumentURL, .move) 似乎无缘
我是一名优秀的程序员,十分优秀!