使用自定义文件包UTI导出UIDocument [英] Export UIDocument with custom file package UTI

查看:330
本文介绍了使用自定义文件包UTI导出UIDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UIDocumentPickerViewController 导出我的 UIDocument 子类。子类将数据写入 FileWrapper ,其UTI符合 com.apple.package

I'm trying to export my UIDocument subclass with a UIDocumentPickerViewController. The subclass writes data to a FileWrapper and its UTI conforms to com.apple.package.

但是显示的文档选择器显示iCloud Drive中的文档无法使用,因为iCloud Drive设置已禁用。

But the presented document picker shows "Documents in iCloud Drive are not available because the iCloud Drive setting is disabled."

文档成功写入缓存,正如我从导出的容器包中看到的那样。

The document is successfully written to the cache, as I can see from the exported container package.

当我更改文档子类和自定义UTI以符合单个文件(例如 public.plain-text )时,文档选择器工作正常,我可以导出文件。所以问题似乎与文档类型或导出的UTI有关。

When I change the document subclass and custom UTI to conform to a single file (e.g. public.plain-text), the document picker works fine and I can export the file. So the problem seems to be with the Document Type or Exported UTI.

我做错了什么或这是一个错误?

Am I doing something wrong or is this a bug?

Info.plist

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array/>
        <key>CFBundleTypeName</key>
        <string>Custom Doc</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.zxzxlch.documentsandbox.customdoc</string>
        </array>
        <key>LSTypeIsPackage</key>
        <true/>
    </dict>
</array>

...

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>com.apple.package</string>
        </array>
        <key>UTTypeDescription</key>
        <string>Custom Doc File</string>
        <key>UTTypeIdentifier</key>
        <string>com.zxzxlch.documentsandbox.customdoc</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>zzz</string>
            </array>
        </dict>
    </dict>
</array>

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)
    }

}


推荐答案

这解决了我的问题:make t他也符合 public.composite-content ,即

This solves my problem: make the UTI also conform to public.composite-content, i.e.

<key>UTTypeConformsTo</key>
<array>
    <string>com.apple.package</string>
    <string>public.composite-content</string>
</array>

我不知道为什么会这样。

I'm not sure why though.

这篇关于使用自定义文件包UTI导出UIDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆