带有 UIDocumentPickerViewController 的 iOS 自定义 UTI/文件类型

KT_*_*KT_ 6 uti ios uidocumentpickerviewcontroller

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

在我的 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>
Run Code Online (Sandbox Code Playgroud)

和:

<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>
Run Code Online (Sandbox Code Playgroud)

我正在用以下方法实例化:

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)
Run Code Online (Sandbox Code Playgroud)

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

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

我希望我做错了什么,但我看不到。

小智 2

你必须放这样的东西:

<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>
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

另外,在您的控制器中,您必须放置如下内容:

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

应该有效