React Native 0.61集成code push 热更新

Code Push React Native 版本

安装code-push
npm install -g code-push-cli

安装RN版code-push插件
npm install --save react-native-code-push

iOS配置

pod install

  1. 打开 AppDelegate.m , 引入
    # import
  2. 查找以下代码行,用于设置生产版本bridge的源 URL:
    return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  3. 替换为:
return [CodePush bundleURL];

在Info.plist中添加CodePushDeploymentKey和对应的key

Android配置
到android/app/build.gradle 头部引入codepush.gradle 和react.gradle

apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"

到MainApplication.java中添加

// 1. Import the plugin class.

import com.microsoft.codepush.react.CodePush;

public class MainApplication extends Application implements ReactApplication {

    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        ...

        // 添加的内容---------------------------
        @Override
        protected String getJSBundleFile() {
            return CodePush.getJSBundleFile();
        }
    // 添加的内容---------------------------
    };
}

打开strings.xml 添加deployment key

你的DeploymentKey

包装app
每次打开app时检查同步index.js

import codePush from "react-native-code-push";

class MyApp extends Component {
}

MyApp = codePush(MyApp);

从后台恢复时同步

let codePushOptions = { checkFrequency: codePush.CheckFrequency.ON_APP_RESUME };

class MyApp extends Component {
}

MyApp = codePush(codePushOptions)(MyApp);

手动同步

let codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };

class MyApp extends Component {
    onButtonPress() {
        codePush.sync({
            updateDialog: true,
            installMode: codePush.InstallMode.IMMEDIATE
        });
    }

    render() {
        return (
            
                
                    Check for updates
                
             
        )
    }
}

MyApp = codePush(codePushOptions)(MyApp);

打印codePush的log
打开 AppDelegate.m , 引入

#import 

在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法内第一行加上:

RCTSetLogThreshold(RCTLogLevelInfo);

你可能感兴趣的:(React Native 0.61集成code push 热更新)