React Native ios打包

開發(fā)React Native的過(guò)程成,js代碼和圖片資源運(yùn)行在一個(gè)Debug Server上,每次更新代碼之后只需要使用command+R鍵刷新就可以看到代碼的更改,這種方式對(duì)于調(diào)試來(lái)說(shuō)是非常方便的。
但當(dāng)我們需要發(fā)布App到App Store的時(shí)候就需要打包,使用離線的js代碼和圖片。這就需要把JavaScript和圖片等資源打包成離線資源,在添加到Xcode中,然后一起發(fā)布到App Strore中。
打包離線資源需要使用命令react-native bundle(注:文中使用的項(xiàng)目名稱為RNIos)

react-native bundle

React Native的react-native bundle命令是用來(lái)進(jìn)行打包的命令,react-native bundle的詳細(xì)命令選項(xiàng)https://github.com/facebook/react-native/blob/master/local-cli/bundle/bundleCommandLineArgs.js
其中我們常使用的一線命令選項(xiàng):

  • --entry-file ,ios或者android入口的js名稱,比如index.ios.js
  • --platform ,平臺(tái)名稱(ios或者android)
  • --dev ,設(shè)置為false的時(shí)候?qū)?huì)對(duì)JavaScript代碼進(jìn)行優(yōu)化處理。
  • --bundle-output, 生成的jsbundle文件的名稱,比如./ios/bundle/index.ios.jsbundle
  • --assets-dest 圖片以及其他資源存放的目錄,比如./ios/bundle

打包命令如下:

react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output ./ios/bundle/index.ios.jsbundle --assets-dest ./ios/bundle

為了方便使用,也可以把打包命令寫到npm script中:

"scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "bundle-ios":"node node_modules/react-native/local-cli/cli.js bundle --entry-file index.ios.js  --platform ios --dev false --bundle-output ./ios/bundle/index.ios.jsbundle --assets-dest ./ios/bundle"

  },

然后運(yùn)行命令直接打包:

npm run bundle-ios

打包結(jié)果:

...
transformed 360/360 (100%)
[8:56:31 PM] <END>   find dependencies (3427ms)
bundle: start
bundle: finish
bundle: Writing bundle output to: ./ios/bundle/index.ios.jsbundle
bundle: Done writing bundle output
bundle: Copying 5 asset files
bundle: Done copying assets

可以看到j(luò)sbundle和資源文件已經(jīng)打包成功。

添加資源

離線包生成完成之后,可以在ios目錄下看到一個(gè)bundle目錄,這個(gè)目錄就是bundle生成的離線資源。
需要在Xcode中添加資源到項(xiàng)目中,必須使用Create folder references的方式添加文件夾.

  1. Add Files to "RNIos"


    add Files
  2. 選擇bundle文件,在option中選擇Create folder references
    選擇文件夾
  3. 添加到項(xiàng)目中的文件夾必須是藍(lán)色


    文件夾必須是藍(lán)色

jsCodeLocation

在ios中AppDelegate里可以看到設(shè)置JavaScript代碼位置的代碼:
Debug Server上的設(shè)置

NSURL *jsCodeLocation;
  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"RNIos"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];

在開發(fā)的過(guò)程中可以在這里配置Debug Server的地址,當(dāng)發(fā)布上線的時(shí)候,就需要使用離線的jsbundle文件,因此需要設(shè)置jsCodeLocation為本地的離線jsbundle。
設(shè)置離線的jsCodeLocation:

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"bundle/index.ios" withExtension:@"jsbundle"];

離線包里的.jsbundle文件是經(jīng)過(guò)優(yōu)化處理的,因此運(yùn)行效率也會(huì)比Debug的時(shí)候更高一些。

項(xiàng)目代碼地址:https://github.com/jjz/react-native/tree/master/RNIos

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容