http://blog.youkuaiyun.com/vaintwyt/article/details/42043269

本文详细介绍如何解决Mac系统GateKeeper对非Mac App Store应用的限制问题,包括使用正确的证书进行代码签名、验证签名及创建安装包的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载博客文章http://blog.youkuaiyun.com/vaintwyt/article/details/42043269,对于解决在mac appstore外分发应用程序有参考价值,亲测可用。

相关命令,访问控制设置:spctl;编译包,xcodebuild;打包安装包 pkgbuild,productbuild;代码签名:codesign,productsign;

Mac安装或者第一次打开软件,经常会出现下面对话框

      


从用户角度,有两种方法解决这个问题。

1.修改安全性与隐私设置,选择任何来源(系统偏好设置-安全性与隐私)



2.右键-打开



从开发者的角度,则要复杂一些了。

首先看看导致这种现象的原因。简单来说是Mac系统的GateKeeper在安装和打开软件时,对软件的来源和开发者进行校验。校验的内容是软件的签名,即软件内部的_CodeSignature目录内容。这是我的理解哈。

   GateKeeper介绍


然后,就应该从软件的签名入手。

签名需要证书,有多种类型:Developer ID Application,Developer ID Installer,3rd Party Mac Developer Application,3rd Party Mac Developer Installer等。证书的生成,这里不做介绍,因为本人也不是很了解。

经验证,对于App,需要使用Developer ID Application进行签名,否则在第一次打开会提示未知开发者。而对于pkg安装包,则要使用Developer ID Installer进行签名,否则在安装的时候,会提示未知开发者。这里证书的选择还是比较重要的,不然,不管你签名是否成功,都过不了GateKeeper那关。LZ就是因为证书选择错了,浪费了好几天时间。

签名可以通过下面命令行实现(如果是Xcode项目,可以直接在Xcode中配置code sign):

  1. # App签名  
  2. codesign --timestamp=none -f -v -s "Developer ID Application" test.app  
  3.   
  4. # pkg签名  
  5. productsign --timestamp=none --sign "Developer ID Installer" ./test.pkg ./test_sign.pkg  

官方给出了 验证签名的方法,只有当结果为accepted的时候,软件才能正常呗打开或者安装

  1. # App  
  2. spctl -a -v --type exec ./test.app  
  3. # pkg  
  4. spctl -a -v --type install ./test_sign.pkg  


官方指引 


 关于软件打包,这里有两个纠结的地方。

以前我都是用PackageManager来打包bundle,生成mpkg文件。虽然现在的Xcode没有附带PackageManager,不过还是可以通过下载旧版Xcode来获得。

我将mpkg文件进行签名,正常来说,安装包应该使用Developer ID Installer证书来签名,但是却一直报错。而当我用Developer ID Application证书来签名时,签名是成功了,却没能解决图一的问题。所以后来只能用命令行来打包。

一开始使用productbuild,将两个bundle作为组建,添加到一个pkg中很顺利,而且也可以正常安装这个pkg。但是,安装对话框中,没有标题显示。所以需要在外部包多一个pkg,通过distribution文件来定义标题和其他属性。



完整命令

  1. export CODE_SIGN="Developer ID Installer"  
  2. productbuild --component ./a.bundle /Library/Internet\ Plug-Ins --component ./b.bundle /Library/Internet\ Plug-Ins --sign "${CODE_SIGN}" test.pkg  
  3. productbuild --distribution distribution.xml --sign "${CODE_SIGN}" ./installer.pkg  

distribution.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <installer-gui-script minSpecVersion="1">  
  3.     <title>软件标题</title>  
  4.     <organization>com.example</organization>  
  5.     <options customize="never" allow-external-scripts="no" rootVolumeOnly="false"/>  
  6.     <!-- List all component packages -->  
  7.     <pkg-ref id="com.example.plugin" version="2005" auth="root" onConclusion="none">test.pkg</pkg-ref>  
  8.     <!-- List them again here. They can now be organized as a hierarchy if you want. -->  
  9.     <choices-outline>  
  10.     <line choice="com.example.plugin"/>  
  11.     </choices-outline>  
  12.     <!-- Define each choice above -->  
  13.     <choice id="com.example.plugin" visible="false" start_visible="false" title="plugin" description="plugin" enabled="false" start_selected="true" selected="true">  
  14.     <!--customLocation="false"-->  
  15.         <pkg-ref id="com.example.plugin"/>  
  16.     </choice>  
  17.     <pkg-ref id="com.example.plugin">  
  18.         <bundle-version/>  
  19.     </pkg-ref>  
  20. </installer-gui-script>  

这时候,令人蛋疼的事情出现了:test.pkg could not be loaded。Google了很久都没有找到解决办法,也尝试在其他电脑上执行上面的命令,也不行。(如果有解决方案,请告诉我。。。)

所以后来混用pkgbuild和productbuild来解决这个问题

  1. export CODE_SIGN="Developer ID Installer"  
  2.   
  3. pkgbuild --identifier "com.example.plugin" --component ./a.bundle --install-location /Library/Internet\ Plug-Ins --component ./b.bundle --install-location /Library/Internet\ Plug-Ins --sign "${CODE_SIGN}" ./test.pkg  
  4.   
  5. productbuild --distribution distribution.xml --sign "${CODE_SIGN}" ./installer.pkg  

总结的时候,感觉不是很难,但是这个问题纠结了好几天,坑。Apple的官方文档说的不明不白的感觉,难道是我英文太差了吗。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值