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

MAUI FilePicker在MacOS提示Operation not permitted问题求助

解决MAUI MacOS下FilePicker沙箱权限报错问题

问题分析

从错误日志来看,核心问题是你配置的沙箱权限键无效——com.apple.app-sandbox.read-write并非苹果沙箱的合法权限键,导致系统无法为文件Picker授予正确的访问扩展权限,最终触发"Operation not permitted"错误。

解决步骤

1. 修正沙箱权限配置

确认你修改的是MacCatalyst项目的Entitlements.plist(MAUI中MacOS沙箱权限默认配置在此文件,而非environments.plist),替换无效权限项:

<dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
    <key>com.apple.security.assets.movies.read-only</key>
    <true/>
    <key>com.apple.security.assets.music.read-only</key>
    <true/>
    <key>com.apple.security.assets.pictures.read-only</key>
    <true/>
    <key>com.apple.security.files.downloads.read-only</key>
    <true/>
    <!-- 替换为正确的用户选择文件权限 -->
    <key>com.apple.security.files.user-selected.read-write</key>
    <true/>
    <key>com.apple.security.personal-information.photos-library</key>
    <true/>
</dict>
  • 移除无效的com.apple.app-sandbox.read-write键;
  • 若仅需读取文件,可保留com.apple.security.files.user-selected.read-only,但错误日志显示系统请求读写扩展,建议先使用读写权限测试。

2. 确保权限文件关联项目

打开MAUI项目的.csproj文件,检查MacCatalyst目标框架下是否正确配置权限文件路径:

<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0-maccatalyst'">
    <CodeSignEntitlements>Platforms/MacCatalyst/Entitlements.plist</CodeSignEntitlements>
</PropertyGroup>

若路径与你的权限文件实际位置不符,修改为正确路径。

3. 清理缓存并重建项目

  • 执行Build > Clean Solution清理项目;
  • 删除项目根目录下的bin和obj文件夹,彻底清除旧缓存;
  • 重新构建并运行项目。

4. 检查系统级权限设置

打开MacOS系统设置 > 隐私与安全性 > 文件与文件夹,找到你的应用,确认已勾选Downloads文件夹的访问权限,未勾选则手动开启。

5. 验证代码文件操作逻辑

确保后续代码直接使用FileResult提供的路径访问文件,不要手动拼接路径或访问未授权的沙箱外文件:

if (file != null)
{
    // 直接使用框架提供的流或路径
    using var stream = await file.OpenReadAsync();
    // 读取/写入操作逻辑
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 10:29:54