6

我有一个自定义文件类型,我试图让我的应用程序从 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>

和:

<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”看起来像这样:

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

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

4

1 回答 1

1

你必须这样写:

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

同样在您的控制器中,您必须输入以下内容:

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

它应该工作

于 2019-09-23T23:48:59.053 回答