使用自定义文件包UTI导出UIDocument
我正在尝试使用UIDocument
导出我的UIDocument
子类。 子类将数据写入FileWrapper
,其UTI符合com.apple.package
。
但是,所提供的文档选择器显示“iCloud Drive中的文档不可用,因为iCloud Drive设置已禁用。”
正如我从导出的容器包中看到的那样,文档已成功写入缓存。
当我更改文档子类和自定义UTI以符合单个文件(例如public.plain-text
)时,文档选择器工作正常,我可以导出文件。 所以问题似乎与文档类型或导出的UTI有关。
我做错了什么或这是一个错误?
的Info.plist
CFBundleDocumentTypes CFBundleTypeIconFiles CFBundleTypeName Custom Doc LSHandlerRank Owner LSItemContentTypes com.zxzxlch.documentsandbox.customdoc LSTypeIsPackage ... UTExportedTypeDeclarations UTTypeConformsTo com.apple.package UTTypeDescription Custom Doc File UTTypeIdentifier com.zxzxlch.documentsandbox.customdoc UTTypeTagSpecification public.filename-extension zzz
CustomDocument.swift
private let textFilename = "contents.txt" class CustomDocument: UIDocument { var content = "Test" override func load(fromContents contents: Any, ofType typeName: String?) throws { guard let topFileWrapper = contents as? FileWrapper, let textData = topFileWrapper.fileWrappers?[textFilename]?.regularFileContents else { return } content = String(data: textData, encoding: .utf8)! } override func contents(forType typeName: String) throws -> Any { let textFileWrapper = FileWrapper(regularFileWithContents: content.data(using: .utf8)!) textFileWrapper.preferredFilename = textFilename return FileWrapper(directoryWithFileWrappers: [textFilename: textFileWrapper]) } }
ViewController.swift
func exportDocument() { // Write to cache let cachesDir = FileManager.default.urls(for: FileManager.SearchPathDirectory.cachesDirectory, in: .allDomainsMask).first! let dataDir = cachesDir.appendingPathComponent("export", isDirectory: true) try! FileManager.default.createDirectory(at: dataDir, withIntermediateDirectories: true, attributes: nil) let fileURL = dataDir.appendingPathComponent("cookie").appendingPathExtension("zzz") let archive = CustomDocument(fileURL: fileURL) archive.content = "Cookie cat" archive.save(to: archive.fileURL, for: .forCreating) { success in guard success else { let alertController = UIAlertController.notice(title: "Cannot export data", message: nil) self.present(alertController, animated: true, completion: nil) return } let documentPicker = UIDocumentPickerViewController(url: archive.fileURL, in: .exportToService) documentPicker.delegate = self self.present(documentPicker, animated: true, completion: nil) } }
这解决了我的问题:使UTI也符合public.composite-content
,即
UTTypeConformsTo com.apple.package public.composite-content
我不确定为什么。