最近很多客户反映,从官网下载的Mac Electron
应用打不开,直接报病毒,类似于这种:

这是因为在MacOS 10.14.5
之后,如果应用没有在苹果官方平台进行公证notarization
(我们可以理解为安装包需要审核,来判断是否存在病毒),那么就不能被安装。当然现在很多人的解决方案都是使用sudo spctl --master-disable
,取消验证模式,但是总体给客户培训下来,效果非常差,很多人找不到终端,有的人甚至不能在终端中输入命令。因此急需找到解决问题的源头,如何进行代码公证。
1. 签名
在App进行公证之前,App首先必须有一个签名,来证明这个app的开发者是谁:
首先你需要有一个苹果开发者账号
首先,找到开发电脑的钥匙串
,找到证书助理
,创建一个从证书机构请求证书
:

填写自己的信息,并保存,生成一个CertificateSigningRequest.certSigningRequest
文件。

现在需要生成 Developer ID Application
证书
登录苹果开发者网站:https://developer.apple.com/account/resources/certificates/add
添加 Developer ID Application
类型证书:

导入上面生成的CertificateSigningRequest.certSigningRequest
文件,导入完成之后即可下载我们需要的证书了:

下载到电脑之后,双击一下,证书就被安装到了钥匙串中了。
此时,打开钥匙串,选择登录
钥匙串,并在我的证书
栏找到刚才导入的证书:

右键并下载,

保存成p12
文件:

保存到你的路劲之后,此时需要配置环境变量:
export CSC_LINK=‘你的p12文件路径'
export CSC_KEY_PASSWORD=‘你创建p12文件的密码’
创建完成之后,此时我们来进行打包,使用 Electron
自带的打包命令:
sudo npm run build
看到这句话之后,就说明签名可用了:

2. 公证
之前使用的altool
工具被苹果打回了,现在只能使用notarytool
工具打包了,具体文档可以查看这篇文章:
https://developer.apple.com/documentation/technotes/tn3147-migrating-to-the-latest-notarization-tool
直接上命令行:
xcrun notarytool submit --force filepath --apple-id "your_apple_id" --password "password" --team-id "team_id"
下面来解释一下所有参数的意义:
- filepath 你生成文件的路劲
- your_apple_id 你的苹果开发者账号
- password 你的应用专属密码,和你的苹果开发者账号密码不一样,可以查阅相关资料设置应用专属密码
- team_id 你的团队id
提交之后,如果有以下结果:
Conducting pre-submission checks for app.zip and initiating connection to the Apple notary service...
Submission ID received
id: ad944394-4ccb-456e-aff5-80e873503e15
Upload progress: 100.00% (91.1 MB of 91.1 MB)
Successfully uploaded file
id: ad944394-4ccb-456e-aff5-80e873503e15
path: /Users/admin/Downloads/app.zip
可以看到,我们提交成功了,已经在预检查状态了。
那么,我们何时可以看到我们的公证状态呢:
xcrun notarytool history --apple-id "your_apple_id" --password "password" --team-id "team_id"
可以得到如下图:

我们可以看到,我们提交的app状态都是Accpeted
,说明公证已经被受理了,现在处于接收状态。目前status
有以下状态:
- Accepted 被接受
- Invalid 无效
- In Progress 进行中
一般提交半小时之内,如果没有异常情况,状态都会由In Progress
变为Accepeted
.说明您的公证已经完成。
一般我们公证完成之后,我们重新下载App并打开,出现的如下图片:

这样,一般对电脑不熟的客户,直接点击打开
就可以直接打开App了,就没有之前那种强制不能开启的逻辑了。
当然了,这是一次记录,如果您有什么疑问,欢迎添加我的wx:javainstalling,暗号:公证,一起解决问题吧。
参考文章:
- https://www.cnblogs.com/lovesong/p/11782449.html
- https://www.cnblogs.com/e0yu/p/14751259.html