install cpcn-react-native & link
,需稍等一会,待执行完毕后,则已自动将所需的依赖包和配置都做好了。
http://code-push.cn
和 https://code-push.cn
的访问。
check(options)
方法即可检测是否有新版本需要更新:
import cpcn from "cpcn-react-native";class App extends React.Component {componentDidMount(){cpcn.check({// 检测是否有新版本后执行此方法。// 如果没有可更新的版本,则 remotePackage 为 null,否则不为 null。// 当 remotePackage 不为 null 时,可提示用户有新版本,// 当用户确认更新后,则调用 agreeContinueFun 方法,并将 true 作为参数传入,表示继续执行更新。checkCallback: (remotePackage, agreeContinueFun) => {if(remotePackage){// TODO: 显示对话框提示用户有新版本可更新// 第二个参数 agreeContinueFun 是 function,// 当用户确认继续执行更新,则调用参数中的 agreeContinueFun 方法,并将 true 作为参数传入。// 也可在任何地方调用 cpcn.agreeContinue(true),它与在此处调用 agreeContinueFun(true) 是等效的。agreeContinueFun(true);}},// 下载开始前调用此方法。该配置是可选的。onDownload: () => {// TODO: do something},// 从服务器下载新版本时执行此方法。该配置是可选的。// 可在此显示下载进度条。// 参数 downloadProgress 里包含两个数据:totalBytes(需下载的总字节大小)、receivedBytes(已下载的字节大小)。downloadProgressCallback: (downloadProgress) => {// TODO: 可用 downloadProgress 数据制作进度条console.log('received ' + downloadProgress.receivedBytes + ' of ' + downloadProgress.totalBytes);},// 当新版本安装完成后执行此方法。该配置是可选的。// 如果不做此配置,则新版本安装完全后将马上重启 App(新版本在重启 App 后才会生效)。// 如果做了此配置,则可调用 restartFun 方法来控制是否重启 App。installedCallback: (restartFun) => {// TODO: do something// 参数 restartFun 是 function,// 若希望重启 App,则调用此方法,并将 true 作为参数传入。restartFun(true);}});}// .....}export default App;
cpcn.check(options)
的参数 options
可参考上例中的注释。