赞
踩
一、首先是打包,mac的打包选项有dmg、mas、zip等,
dmg分为x64、arm64,分别对应intel芯片和apple m1 m2芯片
配置打包项:Mac=>target=>dmg>[arm64,x64]
- buildOption:{
- "mac": {
- "hardenedRuntime": true,
- "icon": "./public/favicon.png",
- "target": [
- {
- target:"dmg",
- arch:['arm64', 'x64']
- }
- ]
- },
- }
这样打包之后,用户下载dmg文件后安装打开,mac会提示该程序不受信任;好多地方说dmg是不需要签名的,但是我觉得dmg下的.app文件照样还是需要求签名(具体在第二步签名中会是说)。
二、签名
发布appstore 打包target需要添加mas,然后对mas属性进行配置,与mac属性同级,
icon:dmg可以用png格式作为icon,mas需要icns格式的icon,刚开始我是这样以为的,打包的时候dmg有图标了、mas上传验证的时候又提示icns文件错误,换成icns dmg又没图标了,后来才知道icns,为什么后边加s;icns是可以吧多个不同尺寸的png整合到一个icns文件下,由系统分配尺寸,托盘、程序坞,启动台都可以用一个icns文件
hardenedRuntime:配置为false 具体为啥没去研究
provisionprofile :为描述文件, apple开发者平台可以生成、下载
entitlements和entitlementInherit mac软件的权限配置文件,包括了沙箱配置、teamid配置和摄像头、录音等权限的配置,文件路径直接使用相对路径就可以,用path.resolve 绝对路径也可以(provisionprofile的文件路径也一样)
- buildOption:{
- "mac": {
- 。。。。
- },
- “mas”:{
- "icon": "./public/favicon.icns",
- "hardenedRuntime": false,
- "type": "distribution",
- "provisioningProfile":”public/provision.provisionprofile",
- "entitlements": resolve("mas-build/entitlements.mas.plist"),
- "entitlementsInherit": resolve("mas-build/entitlements.mas.inherit.plist"),
- }
- }
plist文件
entitlementsInherit:
-
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>com.apple.security.app-sandbox</key>
- <true/>
- <key>com.apple.security.inherit</key>
- <true/>
- </dict>
- </plist>
-
-
entitlements:
-
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>com.apple.security.app-sandbox</key>
- <true/>
- <key>com.apple.security.application-groups</key>
- <array>
- <string>你的teamID</string>
- </array>
- </dict>
- </plist>
-
-
dmg类型的包不需要plist进行配置,直接在mac配置项里添加extandInfo属性就可以配置teamId了
- buildOption:{
- mac:{
- ...,
- "extendInfo":{
- "electronTeamId”:”。。。”//你的teamid
- },
- }
- }
三、签名
首先要有签名文件,生成签名文件需要在apple开发者
1、添加证书:
在accont=>certificates,点击添加证书,
2、证书类型选择:这里需要特别注意,可以选择的类型有Developer ID Application和Apple Distribution类型,mas用的是Apple Distribution类型;他们分别对应了不同的下载安装方式,前者是mac appstore以外的平台比如官网下载,后者是可以提交到mac appstore审核发布下载,我这里是两个都创建了 打包的时候可以看到 dmg的.app文件用的是 Developer ID Application签名;mas用的是Apple Distributioin类型进行签名
3、Cer文件上传:
选择文件类型下一步需要上传cer文件,文件通过mac 的 钥匙串访问 程序生成
--打开mac的“钥匙串访问”程序,屏幕左上角 钥匙串访问=>证书助理=>从正式颁发机构请求证书
--用户电子邮件:appledeveloper的账户邮箱就可以
--CA电子邮件地址:空格就行
--请求:存储到磁盘
生成之后在添加证书cer文件上传步骤进行上传
4、上传完成下一步,下载certificates证书到本地
5、点击打开一下,就可以在钥匙串访问 登录=>证书 中看到了
6、找到钥匙串访问 登录=>该证书,点击左侧小箭头,展开专用密钥,右键导出,记住路径,设置并记住密码
7、配置环境变量,打包签名的时候会按照这个环境变量查找签名文件;
打开命令行输入
sudo vim ~/.bash_profile
输入密码后,任意按键进入insert模式,输入以下内容
export CSC_LINK=/***/***.p12
export CSC_KEY_PASSWORD=***
CSC_LINK 表示你的p12文件地址;CSC_KEY表示 你的p12文件密码,导出的时候设置的密码
四、公证
签名之后需要进行公证,从macOS 10.14.5开始,使用新的开发者ID证书和所有新的或更新的内核扩展签名的软件必须经过公证才能运行。
electron有提供公正工具:electron-notarize,安装依赖
npm install electron-notarize —save;
在buildOption添加afterSign属性,该属性表示打包签名后运行的文件,执行notarization/index.js路径的文件进行公正,路径同样也是相对路径就可以
- builderOptions: {
-
- "afterSign": "notarization/index.js",
- ...
- ...
- }
notarization路径下index.js文件如下:
- const { notarize } = require('electron-notarize');
- const path = require("path")
- exports.default = async function notarizing(context) {
- const { electronPlatformName, appOutDir } = context;
- if (electronPlatformName !== 'darwin') {
- return;
- }
- const appName = context.packager.appInfo.productFilename;
- return await notarize({
- appBundleId: ‘apple开发者中该应用的bundleid',
- appPath: `${appOutDir}/${appName}.app`,//打包后的放置app文件的命名和路径
- appleId: “开发者账号",
- appleIdPassword: "app专属密码”,//在apple开发者里可以对某个app设置专属密码
- });
- };
五、上传
接下来就是上传appstore了,electron不像mac app开发使用oc或者swift语言在xcode中就可以进行开发打包提交一些列操作,electron只能打包.pkg文件,需要借助上传工具,我用的是transporter,可以直接在mac的appstore中找到下载,登录后拖拽上传就可以了
六、其他
1、electron mac端打包需要依赖python2.7,如果报错,下载安装一下python2.7,然后配置一下python环境变量就可以了;如果不想配置环境变量,可以在npm run build命令前加上断言
PYTHON_PYTH=*** npm run build
2、从安装依赖、运行打包、签名、认证,几乎都需要访问waiw,建议**软件一直开着
3、如果需要electron-updater更新,buildOptions配置还需要添加publish属性,不然打包是不会生成yml文件(latest.yml和latest-mac.yml),并且mac下的target属性需要添加“zip”,autoupdate只能下载zip文件,url属性就是服务器放置安装包、配置文件的路径,yml和安装包都需要放在这一路径下,electron-updater会访问这一路径下的***-mac.yml和zip文件,win则是是exe和***.yml文件
- buildOptions:{
- ...,
-
- publish: [
- {
- provider: "generic",
- url: updateUrl,
- },
- ],
-
- ...
- mac:{
- target:[
- {
- target: "dmg",
- arch: ["arm64", "x64"],
- },
- "zip",
- "mas"
- ]
- }
- }

完整代码
- electronBuilder: {
- preload: "src/preload.js",
- customFileProtocol: "./",
- nodeIntegration: true,
- builderOptions: {
- appId: "***",
- productName: "***", //项目名,也是生成的安装文件名,即aDemo.exe
- copyright: "***", //版权信息
- afterSign: "notarization/index.js",
- publish: [
- {
- provider: "generic",
- url: updateUrl,
- },
- ],
- win: {
- icon: "./public/win.ico", //256*256
- target: [
- {
- target: "nsis",
- arch: ["ia32", "x64"],
- },
- ],
- },
- nsis: {
- installerIcon: "./public/favicon.ico", // 安装图标
- uninstallerIcon: "./public/favicon.ico", // 卸载图标
- installerHeaderIcon: "./public/favicon.ico", // 安装时头部图标
- oneClick: false, // 是否一键安装
- allowElevation: true, // 允许请求提升。 如果为false,则用户必须使用提升的权限重新启动安装程序。
- allowToChangeInstallationDirectory: true, // 允许修改安装目录
- },
-
- mac: {
- hardenedRuntime: true,
- icon: "./public/favicon.icns",
- provisioningProfile: "public/***.provisionprofile",
- extendInfo: {
- electronTeamId: "***",
- },
- target: [
- {
- target: "dmg",
- arch: ["arm64", "x64"],
- },
- "zip",
- "mas"
- ],
- },
- dmg: {
- sign: false,
- },
- "mas": {
- "hardenedRuntime": false,
- "type": "distribution",
- "provisioningProfile":"public/***.provisionprofile",
- "entitlements": "mas-build/entitlements.mas.plist",
- "entitlementsInherit": "mas-build/entitlements.mas.inherit.plist",
- },
- },
- },

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。