You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 16.x应用点击自定义附件无法启动,需补充哪些配置?

iOS 16.x 自定义UTI无法关联文件的问题解决

问题描述

我为iOS 16.x应用在Info.plist中定义了自定义UTI,但未达到预期效果:点击带有.custom扩展名的文件链接或附件时,仅显示JSON内容。
奇怪的是,在Mac上将该扩展名文件拖入消息线程时,它能被识别为「App Name Document」类型(Mac可正确关联我在Xcode中开发、运行在真机上的iOS应用)。
但在iOS端的Files应用、消息、邮件,或通过iCloud.com浏览iCloud Drive时,无论点击或长按,都没有打开我的应用的选项,仅显示JSON内容。
我已在AppDelegate.swift和SceneDelegate.swift中实现了open url代理方法。

当前Info.plist配置

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>llc.company.App-Name-document</string>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.json</string>
        </array>
        <key>UTTypeDescription</key>
        <string>App Name Document</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>custom</string>
            </array>
        </dict>
    </dict>
</array>
<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>App Name Document</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>llc.company.App-Name-document</string>
        </array>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
    </dict>
</array>

解决方案

1. 调整UTI继承关系

当前UTI仅继承自public.json,iOS会优先将文件识别为JSON类型,而非你的自定义UTI。建议添加public.data作为父类型,让系统优先识别自定义UTI:

<key>UTTypeConformsTo</key>
<array>
    <string>public.data</string>
    <string>public.json</string>
</array>

2. 补充MIME类型标识

在UTTypeTagSpecification中添加自定义MIME类型,帮助系统更精准识别文件:

<key>UTTypeTagSpecification</key>
<dict>
    <key>public.filename-extension</key>
    <array>
        <string>custom</string>
    </array>
    <key>public.mime-type</key>
    <string>application/vnd.company.app-name-document</string>
</dict>

3. 完善文档类型配置

优化CFBundleDocumentTypes,确保配置更符合iOS的识别逻辑,可补充图标提升辨识度:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>App Name Document</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>llc.company.App-Name-document</string>
        </array>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <!-- 可选:添加文档图标,需提前导入对应资源 -->
        <key>CFBundleTypeIconFiles</key>
        <array>
            <string>DocumentIcon</string>
        </array>
    </dict>
</array>

4. 重置iOS UTI缓存

iOS可能缓存了旧的UTI关联数据,按以下步骤重置:

  • 删除设备上的应用
  • 重启iOS设备
  • 重新安装应用
  • 确认测试文件的扩展名确实是.custom(避免系统隐藏扩展名导致实际为.custom.json)

5. 验证文件UTI识别

在Mac上使用mdls命令检查文件的UTI,确认是否正确关联到你的自定义类型:

mdls -name kMDItemContentType your-file.custom

内容的提问来源于stack exchange,提问作者clearlight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:25:07