iOS 自定义 UTI/文件类型与 UIDocumentPickerViewController

标签 ios uti uidocumentpickerviewcontroller

我有一个自定义文件类型,我试图允许我的应用程序从 UIDocumentPickerViewController 打开。然而,它并没有起作用。 (here 似乎描述了类似的情况和问题,但没有可行的解决方案。)

在我的 Info.plist 中我有:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>myext</string>
        </array>
        <key>CFBundleTypeIconFile</key>
        <string>document.icns</string>
        <key>CFBundleTypeIconFiles</key>
        <array/>
        <key>CFBundleTypeName</key>
        <string>My document</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.mycompany.app.document</string>
        </array>
        <key>NSPersistentStoreTypeKey</key>
        <string>Binary</string>
    </dict>
</array>

和:

<key>UTImportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array/>
        <key>UTTypeDescription</key>
        <string>My document</string>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.app.document</string>
        <key>UTTypeSize320IconFile</key>
        <string>document</string>
        <key>UTTypeSize64IconFile</key>
        <string>document</string>
        <key>public.filename-extension</key>
        <array>
            <string>myext</string>
            <string>MYEXT</string>
        </array>
    </dict>
</array>

我正在实例化事物:

let picker = UIDocumentPickerViewController(documentTypes: ["com.mycompany.app.document"],
                                            in: UIDocumentPickerMode.import)
picker.delegate = self
picker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
self.present(picker, animated: true, completion: nil)

问题是,我的 iCloud 帐户中的“测试文件 I.myext”看起来像这样:

即灰显、不可选择、无法识别的文件。

我希望我做了一些明显错误的事情,但我看不到它。

最佳答案

你必须输入这样的内容:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeIconFiles</key>
                <array/>
                <key>CFBundleTypeName</key>
                <string>My Extension</string>
                <key>CFBundleTypeRole</key>
                <string>Editor</string>
                <key>LSHandlerRank</key>
                <string>Owner</string>
                <key>LSItemContentTypes</key>
                <array>
                    <string>myext</string>
                </array>
            </dict>
        </array>
        <key>CFBundleTypeIconFiles</key>
        <array/>
        <key>CFBundleTypeName</key>
        <string>myext file</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.mycompany.app.document.myext</string>
        </array>
    </dict>
</array>

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>My Extension File</string>
        <key>UTTypeIconFiles</key>
        <array/>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.app.document.myext</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>myext</string>
            </array>
        </dict>
    </dict>
    <dict/>
</array>

此外,您还必须在 Controller 中添加如下内容:

let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.mycompany.app.document.myext"], in: .import)

应该有效

关于iOS 自定义 UTI/文件类型与 UIDocumentPickerViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49375657/

相关文章:

ios - iOS/Swift 项目中自定义统一类型标识符 (UTI) 的图标未显示

ios - 如何使用 AirDrop 发送和接收自定义数据

ios - iOS 未调用 UIDocumentPickerDelegate 方法 (didPickDocumentsAt)

ios - 按钮在 UIDocumentPickerViewController 中不可见

ios - 表格 View 上显示姓名、名字和日期

objective-c - 调用 sleep (5);并更新文本字段不起作用

ios - iOS App 开发的 Swift 用户输入方法

iOS:使用 UIDocumentInteractionController 将特定于应用程序的文件发送到 Dropbox 时出错

ios - 快速 iOS : create UIDocument in app documents folder

ios - 在 swift 中无法使用 MICountry 选择器的 dial_code