问题描述
在Xcode打包并上传应用到App Store Connect时,发现打包报错:

报错信息:
Validation failed
App sandbox not enabled. The following executables must include the "com.apple.security.app-sandbox" entitlement with a Boolean value of true in the entitlements property list: [( "com.fangjunyu.ImageSlim.pkg/Payload/ImageSlim.app/Contents/Resources/pngquant" )] Refer to App Sandbox page at https://developer.apple.com/documentation/security/app_sandbox for more information on sandboxing your app. (ID: 1eda61e5-8611-465e-bc89-40ddec74bd89)
报错原因:应用中包含了一个外部二进制文件 pngquant,它被打包到了 Contents/Resources/ 路径中。但这个可执行文件 pngquant 没有启用 App Sandbox 权限(com.apple.security.app-sandbox = true),而 macOS App Store 要求应用中所有可执行文件(包括主程序和嵌套的工具程序)都必须启用沙盒。
解决方案:
重新编译 pngquant 并签名、启用沙盒:需要将 pngquant 源代码拉下来,用自己的签名证书重新编译并加入沙盒 entitlements。
这里的解决方案,在上一篇问题文章有提到重新签名的流程《Xcode报错:”pngquant” must be rebuilt with support for the Hardened Runtime. Enable the Hardened Runtime capability in the project editor》,这里不提签名的步骤。
当我使用codesign命令检查签名:
codesign -dvvv pngquant // codesign 检查签名
pngquant文件实际上是已经签名的状态:
flags=0x10000(runtime)
验证沙盒权限:
codesign -d --entitlements - /path/to/pngquant // codesign 验证沙盒权限
终端输出:
Executable=/path/to/pngquant // 输出 pngquant 文件路径
这表示签名中没有任何 entitlements(权限声明)。
因此,这个报错的原因在于缺少com.apple.security.app-sandbox 沙盒权限。
解决方案
需要使用entitlements.plist重新签名,首先创建entitlements.plist文件,创建流程可参考《Xcode创建entitlements.plist配置文件》。
创建完成后,配置entitlements.plist文件的字段为:
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.inherit</key>
<true/>
</dict>
配置entitlements.plist文件后,在终端中签名可执行文件:
codesign --force --options runtime \
--entitlements "/path/to/entitlements.plist" \
--sign "Developer ID Application: xxx" \
"/path/to/可执行文件路径"
entitlements.plist和可执行文件都是绝对路径,Developer ID Application证书也已申请的证书名称对应。
签名完成后,终端输出:
/path/to/pngquant: replacing existing signature
表示codesign替换了原来的签名,成功写入了新指定的签名和权限(entitlements)。
使用codesign验证签名是否成功:
codesign -dvvv --entitlements - "/path/to/pngquant"
终端输出:
Executable=/Users/fangjunyu.com/Documents/Apple开发/Mac应用-轻压图片/ImageSlim/ImageSlim/Packages/pngquant
Identifier=pngquant
Format=Mach-O universal (x86_64 arm64)
...
有输出,则表示签名成功,如果没有输出,则表示没有签名成功。
总结
签名成功后,重新进行Xcode打包,即可上传应用。
需要注意的一点,如果我们只给entitlements.plist文件配置com.apple.security.app-sandbox权限,那么我们的二进制文件就会变成独立的沙盒,无法访问主程序的文件系统和读取目录等数据。
必须配置额外配置com.apple.security.inherit,才可以解决这个问题。
相关文章
1、How to sandbox a command line tool?:https://stackoverflow.com/questions/12959958/how-to-sandbox-a-command-line-tool
2、Apple数字签名codesign:https://fangjunyu.com/2025/07/04/apple%e6%95%b0%e5%ad%97%e7%ad%be%e5%90%8dcodesign/
3、Xcode报错:”pngquant” must be rebuilt with support for the Hardened Runtime. Enable the Hardened Runtime capability in the project editor:https://fangjunyu.com/2025/07/14/xcode%e6%8a%a5%e9%94%99%ef%bc%9apngquant-must-be-rebuilt-with-support-for-the-hardened-runtime-enable-the-hardened-runtime-capability-in-the-project-editor/
4、Xcode创建entitlements.plist配置文件:https://fangjunyu.com/2025/07/17/xcode%e5%88%9b%e5%bb%baentitlements-plist%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6/