您的位置:首页 > Web前端 > React

React Native热更新方案

2018-03-30 17:21 459 查看
2中方式codepush和pushy

本博客是用pushy来热更新

前提条件,你有一个rn项目(并且已经搭建好了rn开发环境,比如node.js npm,Xcode等等)

楼主电脑时macOS High Sierra 10.13.1

注意:pushy只能实时更新rn部分代码,如果你的项目是ios+rn混编那么pushy只能更新rn代码,ios部分代码建议看看我的另一个博客用的是JSPatch

链接:https://blog.csdn.net/qq_30211165/article/details/79745464

第一步

cd到你的项目根目录(就是有package.json文件的)

然后执行

npm install -g react-native-update-cli


如果报错价在前面添加sudo

sudo npm install -g react-native-update-cli


会提示你输入密码也就是你的电脑密码

然后在执行命令行

npm install --save react-native-update@具体版本请看下面的表格


版本

react-native-update版本

0.26及以下

1.0.x

0.27 - 0.28

2.x

0.29 - 0.33

3.x

0.34 - 0.45

4.x

0.46及以上

5.x

最后执行

react-native link react-native-update


如果不错像下面这样那么久完成了



我们可以打开ios/android项目中看看是不是有一个叫RCTHotUpdate.xcodeproj如果有的话那么就成功了

运行一下看看我的报了一个“_BZ2_bzRead", referenced from”错误
解决:在工程target的Build Phases->Link Binary with Libraries中加入libz.tbd、libbz2.1.0.tbd


如果自动link不成功我们可以手动link

具体步骤如下

1 在XCode中的Project Navigator里,右键点击Libraries ➜ Add Files to [你的工程名]

2 进入node_modules ➜ react-native-update ➜ ios 并选中RCTHotUpdate.xcodeproj`

3 在XCode中的project navigator里,选中你的工程,在 Build Phases ➜ Link Binary With Libraries 中添加 libRCTHotUpdate.
4000
a

4 继续在Build Settings里搜索Header Search Path,添加$(SRCROOT)/../node_modules/react-native-update/ios

5 Run your project (Cmd+R)

第二步 配置bundle URL

#import <RCTHotUpdate.h>
@interface FirstViewController ()

#if DEBUG
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"FirstApp"
initialProperties:nil
launchOptions:nil];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

self.view=rootView;
#else
jsCodeLocation=[RCTHotUpdate bundleURL];
#endif


第三步:登入与创建应用

首先请在http://update.reactnative.cn注册帐号,然后在你的项目根目录下运行以下命令:

pushy login
email: <输入你的注册邮箱>
password: <输入你的密码>



这会在项目文件夹下创建一个.update文件,注意不要把这个文件上传到Git等CVS系统上。你可以在.gitignore末尾增加一行.update来忽略这个文件。

登录之后可以创建应用。注意iOS平台和安卓平台需要分别创建:

pushycreateApp–platformiosAppName:<输入应用名字>pushycreateApp–platformiosAppName:<输入应用名字> pushy createApp –platform android

App Name: <输入应用名字>

两次输入的名字可以相同,这没有关系。

如果你已经在网页端或者其它地方创建过应用,也可以直接选择应用:

$ pushy selectApp –platform ios

1) 鱼多多(ios)

3) 招财旺(ios)



Enter appId: 输入应用前面的编号(我的12844)


选择或者创建过应用后,你将可以在文件夹下看到update.json文件,其内容类似如下形式:

{

“ios”: {

“appId”: 1,

“appKey”: “<一串随机字符串>”

},

“android”: {

“appId”: 2,

“appKey”: “<一串随机字符串>”

}

}



你可以安全的把update.json上传到Git等CVS系统上,与你的团队共享这个文件,它不包含任何敏感信息。当然,他们在使用任何功能之前,都必须首先输入pushy login进行登录。

至此应用的创建/选择就已经成功了。

第三步:添加热更新功能

获取appKey

检查更新时必须提供你的appKey,这个值保存在update.json中,并且根据平台不同而不同。你可以用如下的代码获取:

完整代码
import React, {
Component,
} from 'react';

import {
AppRegistry,
StyleSheet,
Platform,
Text,
View,
Alert,
TouchableOpacity,
Linking,
} from 'react-native';

import {
isFirstTime,
isRolledBack,
packageVersion,
currentVersion,
checkUpdate,
downloadUpdate,
switchVersion,
switchVersionLater,
markSuccess,
} from 'react-native-update';

import _updateConfig from './update.json';
const {appKey} = _updateConfig[Platform.OS];

class MyProject extends Component {
componentWillMount(){
if (isFirstTime) {
Alert.alert('提示', '这是当前版本第一次启动,是否要模拟启动失败?失败将回滚到上一版本', [
{text: '是', onPress: ()=>{throw new Error('模拟启动失败,请重启应用')}},
{text: '否', onPress: ()=>{markSuccess()}},
]);
} else if (isRolledBack) {
Alert.alert('提示', '刚刚更新失败了,版本被回滚.');
}
}
doUpdate = info => {
downloadUpdate(info).then(hash => {
Alert.alert('提示', '下载完毕,是否重启应用?', [
{text: '是', onPress: ()=>{switchVersion(hash);}},
{text: '否',},
{text: '下次启动时', onPress: ()=>{switchVersionLater(hash);}},
]);
}).catch(err => {
Alert.alert('提示', '更新失败.');
});
};
checkUpdate = () => {
checkUpdate(appKey).then(info => {
if (info.expired) {
Alert.alert('提示', '您的应用版本已更新,请前往应用商店下载新的版本', [
{text: '确定', onPress: ()=>{info.downloadUrl && Linking.openURL(info.downloadUrl)}},
]);
} else if (info.upToDate) {
Alert.alert('提示', '您的应用版本已是最新.');
} else {

a31d
Alert.alert('提示', '检查到新的版本'+info.name+',是否下载?\n'+ info.description, [
{text: '是', onPress: ()=>{this.doUpdate(info)}},
{text: '否',},
]);
}
}).catch(err => {
Alert.alert('提示', '更新失败.');
});
};
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
欢迎使用热更新服务
</Text>
<Text style={styles.instructions}>
这是版本一 {'\n'}
当前包版本号: {packageVersion}{'\n'}
当前版本Hash: {currentVersion||'(空)'}{'\n'}
</Text>
<TouchableOpacity onPress={this.checkUpdate}>
<Text style={styles.instructions}>
点击这里检查更新
</Text>
</TouchableOpacity>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

AppRegistry.registerComponent('MyProject', () => MyProject);


实时热更新

用Xcode打包成ipa然后上传到pushy

pushy uploadIpa <你的ipa路径>

发布新的热更新版本

你可以尝试修改一行代码(譬如将版本一修改为版本二),然后生成新的热更新版本。

$ pushy bundle –platform

Bundling with React Native version: 0.22.2

<各种进度输出>

Bundled saved to: build/output/android.1459850548545.ppk

Would you like to publish it?(Y/N)

如果想要立即发布,此时输入Y。当然,你也可以在将来使用pushy publish –platform 来发布版本。

Uploading [========================================================] 100% 0.0s

Enter version name: <输入版本名字,如1.0.0-rc>

Enter description: <输入版本描述>

Enter meta info: {“ok”:1}

Ok.

Would you like to bind packages to this version?(Y/N)

此时版本已经提交到update服务,但用户暂时看不到此更新,你需要先将特定的包版本绑定到此热更新版本上。

此时输入Y立即绑定,你也可以在将来使用pushy update –platform ios|android来使得对应包版本的用户更新。 除此以外,你还可以在网页端操作,简单的将对应的包版本拖到此版本下即可。

Offset 0

1) FvXnROJ1 1.0.1 (no package)

2) FiWYm9lB 1.0 [1.0]

Enter versionId or page Up/page Down/Begin(U/D/B) <输入序号,U/D翻页,B回到开始,序号就是上面列表中)前面的数字>

1) 1.0(normal) - 3 FiWYm9lB (未命名)

Total 1 packages.

Enter packageId: <输入包版本序号,序号就是上面列表中)前面的数字>

注意:

如果访问极慢或者显示网络失败,请设置使用淘宝镜像(也仅需设置一次):

npm config set registry https://registry.npm.taobao.org –global

npm config set disturl https://npm.taobao.org/dist –global

Xcode报错“_BZ2_bzRead”, referenced from”

在工程target的Build Phases->Link Binary with Libraries中加入libz.tbd、libbz2.1.0.tbd
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: