修改CodePush客户端源码,实现自己的服务器端
参考资料:
-
cd ios && pod install && cd ..
-
AppDelegate.m文件,引入头文件
#import "HotUpdate.h"
-
AppDelegate.m文件,didFinishLaunchingWithOptions生命周期函数中将
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
替换成
return [HotUpdate bundleURL];
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.rickl.reactlibrary.hotupdate.HotUpdate;
to the imports at the top of the file - 在
getPackages()
函数同层级下,增加下面方法@Override protected String getJSBundleFile() { return HotUpdate.getJSBundleFile(); }
- Add
-
In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
-
Go to
node_modules
➜react-native-hot-update
and addHotUpdate.xcodeproj
-
In XCode, in the project navigator, select your project. Add
libHotUpdate.a
to your project'sBuild Phases
➜Link Binary With Libraries
-
AppDelegate.m文件,引入头文件
#import <HotUpdate/HotUpdate.h>
-
AppDelegate.m文件,didFinishLaunchingWithOptions生命周期函数中将
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
替换成
jsCodeLocation = [HotUpdate bundleURL];
- Open up
android/app/src/main/java/[...]/MainApplication.java
-
Add
import com.rickl.reactlibrary.hotupdate.HotUpdate;
to the imports at the top of the file -
Add
new HotUpdate(getApplicationContext(), BuildConfig.DEBUG)
to the list returned by thegetPackages()
method -
在
getPackages()
函数同层级下,增加下面方法@Override protected String getJSBundleFile() { return HotUpdate.getJSBundleFile(); }
- Append the following lines to
android/settings.gradle
:include ':react-native-hot-update' project(':react-native-hot-update').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-hot-update/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-hot-update')
import HotUpdate from 'react-native-hot-update';
class App extends React.Component {
componentDidMount() {
HotUpdate.setServerUrl(`http://xxx.com/project/hotupdate/${Platform.OS}`)
HotUpdate.notifyAppReady()
}
render() {
return (
<View>
<Button title="checkUpdate" onPress={this.checkUpdate} />
</View>
)
}
checkUpdate = () => {
let remotePackage = await HotUpdate.checkUpdate()
if (remotePackage) {
Alert.alert(
'更新提示',
remotePackage.description,
[
{ text: '取消' },
{
text: '更新',
onPress: () => {
HotUpdate.download(remotePackage, ({totalBytes, receivedBytes}) => {
this.setState({ progress: `${receivedBytes} / ${totalBytes}` })
}).then(() => {
Alert.alert('下载完成', '马上重启?',
[
{
text: '稍后',
onPress: () => { HotUpdate.install(remotePackage, HotUpdate.InstallMode.ON_NEXT_RESUME) }
},
{
text: '现在',
onPress: () => { HotUpdate.install(remotePackage, HotUpdate.InstallMode.IMMEDIATE) }
}
]
)
}).catch((e) => {
alert('下载失败: ' + e.toString())
})
},
},
]
)
} else {
alert('暂无更新')
}
}
}
HotUpdate;