ReactNative系列

RN-热更新-pushy

2017-06-02  本文已影响382人  酷酷的开发匠

参考官网

npm install -g react-native-update-cli rnpm
npm install --save react-native-update
react-native link react-native-update
touch react-native-update.podspec
require "json"
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
Pod::Spec.new do |s|
s.name = "react-native-update"
s.version = package["version"]
s.summary = "hot update for react-native"
s.author = "author (https://github.com/reactnativecn)"
s.homepage = "https://github.com/reactnativecn/react-native-pushy"
s.license = "MIT"
s.platform = :ios, "7.0"
s.source = { :git => "https://github.com/reactnativecn/react-native-pushy.git", :tag => "#{s.version}" }
s.source_files = "ios/**/*.{h,m,c}"
s.libraries = "bz2"
s.dependency "React"
end
pod 'react-native-update' , :path => './node_modules/react-native-update'
      pod update
#import "RCTHotUpdate.h"
#if DEBUG
  // 原来的jsCodeLocation
  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
#else
  jsCodeLocation=[RCTHotUpdate bundleURL];
#endif
<key>NSAppTransportSecurity</key>
<dict>
   <key>NSExceptionDomains</key>
   <dict>
       <key>reactnative.cn</key>
       <dict>
           <key>NSIncludesSubdomains</key>
           <true/>
           <key>NSExceptionAllowsInsecureHTTPLoads</key>
           <true/>
       </dict>
  </dict>
</dict>
image
pushy login
email: <输入你的注册邮箱>
password: <输入你的密码>
pushy createApp --platform ios
App Name: <输入应用名字>
import {
 Platform,
} from 'react-native';
import _updateConfig from './update.json';
const {appKey} = _updateConfig[Platform.OS];
import {
 Linking,
} from 'react-native';
import {
 isFirstTime,
 isRolledBack,
 packageVersion,
 currentVersion,
 checkUpdate,
 downloadUpdate,
 switchVersion,
 switchVersionLater,
 markSuccess,
} from 'react-native-update';
  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 {
       Alert.alert('提示', '检查到新的版本'+info.name+',是否下载?\n'+ info.description, [
         {text: '是', onPress: ()=>{this.doUpdate(info)}},
         {text: '否',},
       ]);
     }
   }).catch(err => { 
     Alert.alert('提示', '更新失败.');
   });
 };
  if (isFirstTime) {
     Alert.alert('提示', '这是当前版本第一次启动,是否要模拟启动失败?失败将回滚到上一版本', [
       {text: '是', onPress: ()=>{throw new Error('模拟启动失败,请重启应用')}},
       {text: '否', onPress: ()=>{markSuccess()}},
     ]);
   } else if (isRolledBack) {
     Alert.alert('提示', '刚刚更新失败了,版本被回滚.');
   }
 }
react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output release_ios/main.jsbundle --assets-dest release_ios/
pushy uploadIpa <your-package.ipa>
pushy bundle --platform ios
$ pushy bundle --platform <ios|android>
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
 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)
输入Y
9168) 2.3.0(normal) (newest)
Total 1 packages.
Enter packageId: 9168
Ok.
上一篇下一篇

猜你喜欢

热点阅读