|
|
|
CodePush(后面有些地方简称CP)
|
|
|
|
本wiki文档记录了如何通过CP对Cordova的iOS项目进行热更新。
|
|
|
|
本文档是对另一个文档「CodePush研究记录」的进一步整理。前者主要记录详细的研究过程,也记录了研究过程中的一些错误尝试,内容相对混乱。本文档记录的是具体的整理过后的操作流程。
|
|
|
|
|
|
|
|
# 环境信息
|
|
|
|
本文档记录了CodePush-iOS项目的基本创建步骤,而项目是在特定的环境下运作的,下面是关于项目的环境的一些基本信息。
|
|
|
|
|
|
|
|
设备:MacBook 12-inch 2017
|
|
|
|
OS:macOS 10.15 Catalina
|
|
|
|
|
|
|
|
本记录中安装的工具版本:
|
|
|
|
code-push: 2.1.9
|
|
|
|
|
|
|
|
# 安装CP CLI工具
|
|
|
|
安装CP CLI工具。安装该工具以后可以通过`code-push`命令进行热更新管理。
|
|
|
|
```shell
|
|
|
|
npm install -g code-push-cli
|
|
|
|
```
|
|
|
|
|
|
|
|
如果已经拥有AppCenter账号,可以通过下面命令登录账号。
|
|
|
|
```shell
|
|
|
|
code-push login
|
|
|
|
```
|
|
|
|
命令会在浏览器打开网页进行登录。登录成功以后会返回一个token,在终端输入该token完成终端的登录。
|
|
|
|
|
|
|
|
如果还没有AppCenter账号,则可以通过下面命令进行注册。
|
|
|
|
```shell
|
|
|
|
code-push register
|
|
|
|
```
|
|
|
|
注册成功以后同样会生成token,把token输入终端以完成注册及登录。
|
|
|
|
|
|
|
|
# 创建Cordova项目
|
|
|
|
创建项目
|
|
|
|
```shell
|
|
|
|
cordova create CodePush-iOS com.easternphoenix.CodePush CodePush-iOS
|
|
|
|
```
|
|
|
|
|
|
|
|
进入项目根目录,创建`build.json`
|
|
|
|
```shell
|
|
|
|
cd CodePush-iOS
|
|
|
|
touch ./build.json
|
|
|
|
open -a xcode ./build.json
|
|
|
|
```
|
|
|
|
|
|
|
|
向文件添加下面内容:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"ios": {
|
|
|
|
"debug": {
|
|
|
|
"codeSignIdentity": "iPhone Developer",
|
|
|
|
"automaticProvisioning": true,
|
|
|
|
"developmentTeam": "2WT78RG4ZP",
|
|
|
|
"packageType": "development"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
通过配置`build.json`文件可以控制cordova如何生成iOS项目。
|
|
|
|
|
|
|
|
添加iOS平台
|
|
|
|
```shell
|
|
|
|
cordova platform add ios
|
|
|
|
```
|
|
|
|
|
|
|
|
至此CodePush-iOS项目的iOS平台创建完毕。可以执行下面命令用Xcode打开。
|
|
|
|
```shell
|
|
|
|
open -a xcode platforms/ios/CodePush-iOS.xcworkspace
|
|
|
|
```
|
|
|
|
用Xcode运行一下项目,确保项目可以正常运行即可。
|
|
|
|
|
|
|
|
|
|
|
|
# 安装配置CP热更新
|
|
|
|
在AppCenter注册App
|
|
|
|
```shell
|
|
|
|
code-push app add CodePush-iOS ios cordova
|
|
|
|
```
|
|
|
|
上面命令中,
|
|
|
|
`CodePush-iOS`为在AppCenter注册的App的名称;
|
|
|
|
`ios cordova`指定App的开发平台,这里为iOS和cordova。可能的选项还有android,web,react-native, windows等等。
|
|
|
|
|
|
|
|
在注册成功以后,CLI工具会打印出App的开发开发和发布的key,类似下面这样。
|
|
|
|
![C6EAE9C6-0AB2-4B33-AAEE-AB30E8B52350](/uploads/a626ccce9e69e3c5edf2fb269dbc0dd2/C6EAE9C6-0AB2-4B33-AAEE-AB30E8B52350.png)
|
|
|
|
|
|
|
|
向项目添加CP插件
|
|
|
|
```shell
|
|
|
|
cordova plugin add cordova-plugin-code-push@latest
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
配置`config.xml`
|
|
|
|
```shell
|
|
|
|
open -a xcode ./config.xml
|
|
|
|
```
|
|
|
|
|
|
|
|
把下面内容添加到config.xml的iOS平台的配置中:
|
|
|
|
```xml
|
|
|
|
<platform name="ios">
|
|
|
|
<preference name="CodePushDeploymentKey" value="FLSdRNnDltq7oepp55e-zS4jN-LS5d************" />
|
|
|
|
...
|
|
|
|
</platform>
|
|
|
|
```
|
|
|
|
上面用的是Staging的key,如果真正发布则需要填入Production的key>
|
|
|
|
|
|
|
|
---
|
|
|
|
配置`index.html`
|
|
|
|
```shell
|
|
|
|
open -a xcode ./www/index.html
|
|
|
|
```
|
|
|
|
|
|
|
|
把文件中的含有「`http-equiv="Content-Security-Policy"`」配置的`<meta>`元素替代成下面这样
|
|
|
|
```html
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Security-Policy" content="default-src https://codepush.appcenter.ms 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *;">
|
|
|
|
...
|
|
|
|
</head>
|
|
|
|
...
|
|
|
|
</html>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
在`index.js`添加热更新代码
|
|
|
|
```shell
|
|
|
|
open -a xcode ./www/js/index.js
|
|
|
|
```
|
|
|
|
CP有若干管理热更新的API,最简单是调用`codePush.sync()`方法进行热更新。我把这段代码放置在`deviceready`事件当中,在App打开的时候出发热更新。
|
|
|
|
```js
|
|
|
|
onDeviceReady: function() {
|
|
|
|
this.receivedEvent('deviceready');
|
|
|
|
console.log('=========codePush.sync()'); //打印一下确认被调用
|
|
|
|
codePush.sync(); //添加这段代码触发热更新
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
重新build项目
|
|
|
|
```shell
|
|
|
|
cordova build ios
|
|
|
|
```
|
|
|
|
|
|
|
|
这里再用Xcode运行一下,看CP有没有正常运行。如果CP正常运行的话,控制台应该会打印类似下面这样的信息:
|
|
|
|
```shell
|
|
|
|
2019-10-17 14:35:15.975783+0800 CodePush-iOS[968:213495] Received Event: deviceready
|
|
|
|
2019-10-17 14:35:15.975959+0800 CodePush-iOS[968:213495] =========codePush.sync()
|
|
|
|
2019-10-17 14:35:16.221349+0800 CodePush-iOS[968:213495] [CodePush] Checking for update.
|
|
|
|
2019-10-17 14:35:17.729972+0800 CodePush-iOS[968:213495] [CodePush] App is up to date.
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
# 推送热更新
|
|
|
|
|
|
|
|
在iOS的`设备`上运行一个CodePush-iOS。后面将修改代码并推送更新到已安装过App的设备上。
|
|
|
|
|
|
|
|
使用`设备`是因为后面修改代码后将会在`模拟器`预览,在预览的过程中新版本就已经在`模拟器`上运行使得无法观测到热更新的过程。
|
|
|
|
|
|
|
|
如果以`模拟器`作为测试对象,则在修改代码以后应以`浏览器`进行预览观察。
|
|
|
|
|
|
|
|
---
|
|
|
|
修改`index.xml`的代码
|
|
|
|
```html
|
|
|
|
<html>
|
|
|
|
...
|
|
|
|
<body>
|
|
|
|
<div class="app">
|
|
|
|
<h1>Apache Cordova [New Version]</h1>
|
|
|
|
...
|
|
|
|
</div>
|
|
|
|
...
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
修改`config.xml`的App版本(`vsersion`):
|
|
|
|
```xml
|
|
|
|
<?xml version='1.0' encoding='utf-8'?>
|
|
|
|
<widget id="com.easternphoenix.CodePush" version="1.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
|
|
|
|
...
|
|
|
|
</widget>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
刷新代码,并(在模拟器或者浏览器)预览一下
|
|
|
|
```shell
|
|
|
|
cordova prepare
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
查询更新记录(应该没有任何记录):
|
|
|
|
```shell
|
|
|
|
code-push deployment history CodePush-iOS Staging
|
|
|
|
```
|
|
|
|
|
|
|
|
推送更新
|
|
|
|
```shell
|
|
|
|
code-push release-cordova CodePush-iOS ios -t "~1.0.0" -m true
|
|
|
|
```
|
|
|
|
|
|
|
|
# 备忘
|
|
|
|
在本项目测试过程中出现过一些与基本流程无关的报错。这些流程主要与环境、平台、软件相关。这个部分记录了其中一些,以及它的解决,以防在复制测试的时候有同样的情况出现。
|
|
|
|
|
|
|
|
**报错1**
|
|
|
|
```shell
|
|
|
|
Jerry-MacBook:CodePush-iOS jerry$ cordova build ios
|
|
|
|
CordovaError: Promise rejected with non-error: "xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance\n"
|
|
|
|
at /usr/local/lib/node_modules/cordova/bin/cordova:29:15
|
|
|
|
at processTicksAndRejections (internal/process/task_queues.js:93:5)
|
|
|
|
```
|
|
|
|
解决
|
|
|
|
|
|
|
|
[https://forum.ionicframework.com/t/xcode-select/158410](https://forum.ionicframework.com/t/xcode-select/158410)
|
|
|
|
设置xcode command line tools
|
|
|
|
|
|
|
|
---
|
|
|
|
**报错2**
|
|
|
|
```shell
|
|
|
|
Jerry-MacBook:CodePush-iOS jerry$ cordova build ios
|
|
|
|
Reading build config file:
|
|
|
|
CordovaError: Promise rejected with non-error: "ios-deploy was not found. Please download, build and install version 1.9.2 or greater from https://github.com/ios-control/ios-deploy into your path, or do 'npm install -g ios-deploy'"
|
|
|
|
at /usr/local/lib/node_modules/cordova/bin/cordova:29:15
|
|
|
|
at processTicksAndRejections (internal/process/task_queues.js:93:5)
|
|
|
|
```
|
|
|
|
解决
|
|
|
|
|
|
|
|
拔出ios设备再运行命令(ios-deploy,似乎还没支持macOS Catalina) |
|
|
|
\ No newline at end of file |