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

如何在UIDocumentBrowserViewController中完全隐藏非JPEG格式文件?

如何在UIDocumentBrowserViewController中完全隐藏不支持的文件类型

要彻底隐藏非JPEG文件,你需要完成以下几步配置:

1. 设置允许的内容类型

在你的UIDocumentBrowserViewController子类的viewDidLoad方法中,直接指定应用支持的文件类型:

iOS 14+(使用UTType框架)

import UniformTypeIdentifiers

class JPEGBrowserViewController: UIDocumentBrowserViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // 仅允许JPEG文件
        allowedContentTypes = [UTType.jpeg]
        // 启用标准浏览器样式,确保过滤规则生效
        browserUserInterfaceStyle = .browser
        delegate = self
    }
}

iOS 13及以下(使用UTI字符串)

class JPEGBrowserViewController: UIDocumentBrowserViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // 仅允许JPEG文件对应的UTI
        allowedContentTypes = ["public.jpeg"]
        browserUserInterfaceStyle = .browser
        delegate = self
    }
}

2. 完善Info.plist文档类型配置

确保项目的Info.plist正确声明支持的文档类型,避免系统识别异常:

  • 添加CFBundleDocumentTypes数组项
  • 在数组内添加一个字典,包含以下键值对:
    • CFBundleTypeName:填写JPEG Image(自定义名称)
    • LSItemContentTypes:数组类型,填入public.jpeg
    • LSHandlerRank:设置为Owner或Alternate

3. 自定义过滤规则(可选)

如果需要更灵活的文件过滤逻辑,可以使用filterPredicate属性,比如针对文件后缀名过滤:

filterPredicate = NSPredicate(format: "fileExtension IN %@", ["jpg", "jpeg"])

完成以上配置后,UIDocumentBrowserViewController就只会显示JPEG格式的文件,完全隐藏其他类型文件。

内容的提问来源于stack exchange,提问作者Torrontés

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 14:12:12