gpt4 book ai didi

ios - 使用 UIDocumentBrowserViewController 创建文档

转载 作者:可可西里 更新时间:2023-11-01 04:12:10 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com