首页 > 解决方案 > 使用 UIDocumentPickerViewController initForOpeningContentTypes 时无法选择文件

问题描述

我正在尝试使用 iOS 14 initForOpeningContentTypes:而不是已弃用的initWithDocumentTypes:inMode:,并且我无法访问带有扩展名的文件p8(它们是灰色的)。

我正在尝试以下代码:

NSArray<UTType*> * contentTypes = @[[UTType typeWithFilenameExtension: @"p8"
                                                     conformingToType: UTTypeData]];
UIDocumentPickerViewController *documentPicker = nil;
documentPicker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes: contentTypes];

选择器启动,但文件“myfile.p8”显示为灰色且无法选择。我在这里做错了什么?

标签: objective-cios14uidocumentpickerviewcontroller

解决方案


一旦我将以下内容添加到我的应用程序的 Info.plist 中,我就可以选择该文件:

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>com.my-company.my-identifier</string>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>My Description</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>p8</string>
            </array>
        </dict>
    </dict>
</array>

显然它告诉系统类型。有关. _ _UTExportedTypeDeclarations


推荐阅读