我們可以用React Native提供的命令:react-native bundle
Options:
--entry-file Path to the root JS file, either absolute or relative to JS root [required]
--platform Either "ios" or "android"
--transformer Specify a custom transformer to be used (absolute path) [default: "/Users/babytree-mbp13/projects/xcodeProjects/AwesomeProject/node_modules/react-native/packager/transformer.js"]
--dev If false, warnings are disabled and the bundle is minified [default: true]
--prepack If true, the output bundle will use the Prepack format. [default: false]
--bridge-config File name of a a JSON export of __fbBatchedBridgeConfig. Used by Prepack. Ex. ./bridgeconfig.json
--bundle-output File name where to store the resulting bundle, ex. /tmp/groups.bundle [required]
--bundle-encoding Encoding the bundle should be written in ([https://nodejs.org/api/buffer.html#buffer_buffer).](https://nodejs.org/api/buffer.html#buffer_buffer).) [default: "utf8"]
--sourcemap-output File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map
--assets-dest Directory name where to store assets referenced in the bundle
--verbose Enables logging [default: false]
其實用Xcode打開后可以開到main.js文件奔害,不過是紅色狀態(tài)的笔呀,因為引用了但是并沒有這個文件存在寸谜,React Native這樣做的目的可能就是提示你還沒有生成離線數(shù)據(jù)的意思吧缸沃。
??所以我們這里的bundle-output就選擇這個main.js杉允,如果你想用別的名字膳犹,理論上是可以的终蒂,但是會提示找不到這個文件蜂林,你需要先生成一個空的文件就可以了。
??--assets-dest選擇./ios就可以了拇泣,因為它會幫你在./ios下生成assets文件夾噪叙。
react-native bundle --entry-file index.ios.js --bundle-output ./ios/main.jsbundle --platform ios --assets-dest ./ios --dev false
準備就緒,開始打包霉翔。打包完后你就會看到main.js文件變?yōu)橐延袪顟B(tài)了睁蕾。
??然后打開Appdelegate.m文件,修改:
//jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
然后再把assets文件夾添加到工程中债朵,注意的是必須選擇Creat folder references否則運行時將會報錯子眶,無法找到文件。
最后command+R運行序芦,發(fā)現(xiàn):
出現(xiàn)這樣的原因是因為Build Phases中Copy Bundle Resources沒有添加我們剛才生成的main.js臭杰。
再次command+R運行,跑起來了谚中。