作者:mobiledu2502921033 | 来源:互联网 | 2023-01-28 10:05
文档documentBrowser(_:didRequestDocumentCreationWithHandler:)
说:"创建一个新文档并将其保存到临时位置.如果使用UIDocument
子类创建文档,则必须在调用importHandler
块之前将其关闭."
所以我通过获取用户临时目录(FileManager.default.temporaryDirectory
)的URL 并附加名称和扩展名(获取类似"file:/// private/var/mobile/Containers/Data/Application/C1DE454D-EA1E-的路径)来创建文件URL 4166-B137-5B43185169D8/TMP/Untitled.uti").但是当我调用save(to:for:completionHandler:)
传递此URL时,永远不会回调完成处理程序.我也尝试使用url(for:in:appropriateFor:create:)
在用户的临时目录中传递一个子目录 - 仍然没有调用完成处理程序.
我理解文档浏览器视图控制器由一个单独的进程管理,该进程有自己的读/写权限.除此之外,我很难理解问题所在.临时保存新文档的位置,以便文档浏览器进程可以移动它们?
更新:截至目前的测试版,我现在看到一个错误的域NSFileProviderInternalErrorDomain
和代码1
被记录:"不允许读者访问该URL." 至少那是对发生的事情的确认......
1> Ashley Mills..:
因此,首先,如果您使用的是自定义UTI,则必须正确设置.我看起来像这样......
CFBundleDocumentTypes
CFBundleTypeIconFiles
icon-file-name // Can be excluded, but keep the array
CFBundleTypeName
Your Document Name
CFBundleTypeRole
Editor
LSHandlerRank
Owner
LSItemContentTypes
com.custom-uti
和
UTExportedTypeDeclarations
UTTypeConformsTo
public.data // My doc is saved as Data, not a file wrapper
UTTypeDescription
Your Document Name
UTTypeIdentifier
com.custom-uti
UTTypeTagSpecification
public.filename-extension
doc-extension
也
UISupportsDocumentBrowser
我继承UIDocument
的MyDocument
,并添加下面的方法来创建一个新的临时文件...
static func create(completion: @escaping Result -> 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
}
}
我的委托方法如下:
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
}
}
}
这就是它.让我知道你是怎么办的!