iOS React Native 应用程序不从 CodePush 服务器更新

iOS react native app does not update from CodePush server

我已按照说明正确设置 CodePush。首先,我推送到 AppCenter:

$ appcenter codepush release-react -a myUser/appName -d Production
Detecting ios app version:

Using the target binary version value "1.0" from "ios/reactnative/Info.plist".

Running "react-native bundle" command:

node node_modules/react-native/local-cli/cli.js bundle --assets-dest /var/folders/gy/737c70fj7tq3pwdw758glxz00000gp/T/code-push118924-95814-1e6qqdj.df3g/CodePush --bundle-output /var/folders/gy/737c70fj7tq3pwdw758glxz00000gp/T/code-push118924-95814-1e6qqdj.df3g/CodePush/main.jsbundle --dev false --entry-file index.js --platform ios
Loading dependency graph, done.
bundle: Writing bundle output to: /var/folders/gy/737c70fj7tq3pwdw758glxz00000gp/T/code-push118924-95814-1e6qqdj.df3g/CodePush/main.jsbundle
bundle: Done writing bundle output
bundle: Copying 6 asset files
bundle: Done copying assets

Releasing update contents to CodePush:

Successfully released an update containing the "/var/folders/gy/737c70fj7tq3pwdw758glxz00000gp/T/code-push118924-95814-1e6qqdj.df3g/CodePush" directory to the "Production" deployment of the "appName" app.

其次,我去AppCenter网站,select成功推送的版本,对每个应用程序进行强制推出。

我已尝试重启应用程序、重新安装应用程序等...但应用程序未从 CodePush 服务器更新。

在我的 XCode 控制台中,我看到:

loading - codepush2018-10-24 12:10:10.837955+0200 reactnative[5546:3916480] 
[CodePush] Loading JS bundle from file:///Users/myUser/Library/Developer/CoreSimulator/Devices/4E7D0B07-B6B2-4D3C-B5B8-F28F3DA47077/data/Containers/Bundle/Application/081FD17C-B182-488D-B2D1-C786A3B25AAB/reactnative.app/main.jsbundle

这是否意味着CodePush正在从本地加载?

我相信我 运行 release 方案是正确的:

class App extends React.Component {
    constructor(props){
        super(props)
    }
    componentDidMount() {
        codePush.sync({
            updateDialog: true,
            installMode: codePush.InstallMode.IMMEDIATE
        });
    }
    render(){
        return <Main screenProps={this.props} />
    }
}
const codePushOptions = { checkFrequency: codePush.CheckFrequency.ON_APP_RESUME };
const ConnectedApp = codePush(codePushOptions)(connect(state => state)(App))

firebase.auth().onAuthStateChanged(auth => {
    if(auth){
        store.dispatch(setUser(auth))
    }else{
        store.dispatch(logout())
    }
})

export default () => (
    <Provider store={store}> 
        <ConnectedApp />
    </Provider>
)

我也没有看到 updateDialog: true 应用程序安装时。

CodePush 并非从确切的位置加载,而是从 CodePush 服务器下载包。所以你看到的路径是模拟器文件系统的路径部分。

请参阅此处了解如何设置部署目标

https://docs.microsoft.com/en-us/appcenter/distribution/codepush/react-native

The Code Push keys must be same to the environment to which the application is being released and the one to which the application is built in order to receive the changes.

为了避免这种冲突,有两种可能的解决方案。

  • 定义单独的构建方案。
  • 添加自定义脚本,随环境变化更改代码推送键。

构建方案/类型

Android

android {
    ...
    buildTypes {
        releaseStaging {
            ...
            buildConfigField "String", "CODEPUSH_KEY", '"<INSERT_STAGING_KEY>"'
            ...
        }
        release {
            ...
            buildConfigField "String", "CODEPUSH_KEY", '"<INSERT_PRODUCTION_KEY>"'
            ...
        }
    }
    ...
}

Source

android/app/build.gradle 中您可以定义各种 buildTypes 并基于它构建应用程序

IOS

Info 选项卡下,单击 ConfigurationsDuplicate "Release" Configuration 中的 + 按钮并将其名称设置为 Staging

现在您可以配置 User Defined Settings 并定义新设置 CODEPUSH_KEY 并将 ${CODEPUSH_KEY} 变量设置为不同的方案。

这里解释了详细的步骤:Source

自定义脚本

或者,如果您不想添加更多构建方案/类型,您可以添加一个自定义脚本,根据环境更改密钥。

变化-environment.sh

#!/usr/bin/env bash

#################################
# Change the environment variable
#################################

environment=
# Read Directory Path
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$parent_path"
rm ../.env
# Set NODE_ENV in your .env file based on the script
echo "NODE_ENV=${environment}" > ../.env


#################################
# Change the code push key
#################################

if [ "$environment" = "staging" ]
then
    echo "Change Code Push Key - Staging"
    sed -i '' -E 's/CODEPUSH_KEY ?= ?".+"/CODEPUSH_KEY = "${YOUR_STAGING_IOS_CODEPUSH_KEY}"/' ../ios/YourProj.xcodeproj/project.pbxproj
    sed -i '' -E 's/buildConfigField "String", "CODEPUSH_KEY", '\''".+"'\''/buildConfigField "String", "CODEPUSH_KEY", '\''"${YOUR_STAGING_ANDROID_CODEPUSH_KEY}"'\''/' ../android/app/build.gradle
elif [ "$environment" = "production" ]
then
    echo "Change Code Push Key - Production"
    sed -i '' -E 's/CODEPUSH_KEY ?= ?".+"/CODEPUSH_KEY = "${YOUR_PRODUCTION_IOS_CODEPUSH_KEY}"/' ../ios/YourProj.xcodeproj/project.pbxproj
    sed -i '' -E 's/buildConfigField "String", "CODEPUSH_KEY", '\''".+"'\''/buildConfigField "String", "CODEPUSH_KEY", '\''"${YOUR_PRODUCTION_ANDROID_CODEPUSH_KEY}"'\''/' ../android/app/build.gradle
fi

现在你可以运行.${SCRIPT_PATH}/change-environment.sh ${ENV}

示例:./change-environment.sh staging

此脚本将 android 中的 buildConfigField buildTypesCODEPUSH_KEY User Defined Settings 更改为中的预定义方案ios.

将此脚本与 package.json

中的 react native 打包程序启动命令结合起来会很方便
"start": "./change-environment.sh development && node node_modules/react-native/local-cli/cli.js start",
"start:staging": "./change-environment.sh staging && node node_modules/react-native/local-cli/cli.js start",
"start:production": "./change-environment.sh production && node node_modules/react-native/local-cli/cli.js start",