我們可以用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]
其實(shí)用Xcode打開后可以開到main.js文件,不過是紅色狀態(tài)的,因?yàn)橐昧说遣]有這個(gè)文件存在,React Native這樣做的目的可能就是提示你還沒有生成離線數(shù)據(jù)的意思吧。??所以我們這里的bundle-output就選擇這個(gè)main.js,如果你想用別的名字,理論上是可以的,但是會(huì)提示找不到這個(gè)文件,你需要先生成一個(gè)空的文件就可以了。??--assets-dest選擇./ios就可以了,因?yàn)樗鼤?huì)幫你在./ios下生成assets文件夾。
第一步
react-native bundle --entry-file index.ios.js --bundle-output ./ios/main.jsbundle --platform ios --assets-dest ./ios --dev false
如果你是4.9版本以上使用一下命令:
react-native bundle --entry-file index.js --bundle-output ./ios/main.jsbundle --platform ios --assets-dest ./ios --dev false
準(zhǔn)備就緒,開始打包。打包完后你就會(huì)看到main.js文件變?yōu)橐延袪顟B(tài)了。??然后打開Appdelegate.m文件,修改:

//jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
然后再把a(bǔ)ssets文件夾添加到工程中,注意的是必須選擇Creat folder references否則運(yùn)行時(shí)將會(huì)報(bào)錯(cuò),無法找到文件。

assets文件夾
第二步運(yùn)行環(huán)境下的編譯設(shè)置(Debug/Release)

最后command+R運(yùn)行,發(fā)現(xiàn):程序崩潰了
出現(xiàn)這樣的原因是因?yàn)锽uild Phases中Copy Bundle Resources沒有添加我們剛才生成的main.js。

再次
command+R運(yùn)行,跑起來了。之后正常走Xcode打包發(fā)布流程就好了。
如果按照我這個(gè)步驟來還是不成功,給個(gè)??,私信我?guī)湍悖?
遇到錯(cuò)誤1:
如果你打包IPA 安裝到真機(jī)上時(shí)遇到這個(gè)錯(cuò)誤
:
dyld: Library not loaded: @rpath/RSKImageCropper.framework/RSKImageCropper
Referenced from: /var/containers/Bundle/Application/04722779-7AB0-40C4-961E-4EED77C876A1/RichMedia1.app/RichMedia1
Reason: image not found

在項(xiàng)目的General->Embedded Binaries中,將我們自定的framework 添加進(jìn)去,如下圖:

錯(cuò)誤二:
can't link with a main executable file for architecture armv7
解決:
在 target 下有個(gè) test 工程,
找到 General,然后會(huì)看到一個(gè) testing
在 testing 下有個(gè)
Allow testing Host Application APIs ,它默認(rèn)是勾上的,把勾去掉,
歡迎大家加群討論
點(diǎn)擊鏈接加入群聊【ReactNative技術(shù)交流群3】:1085660877
相關(guān)文章:
1.React Native開發(fā)錯(cuò)誤警告處理總結(jié)(已解決 !持續(xù)更新)
2.ReactNative 實(shí)現(xiàn)的文件上傳功能(實(shí)測(cè)可行!)
3.React Native開發(fā)總結(jié)之:開發(fā)技巧和調(diào)試技巧
4.React Native開發(fā)總結(jié):一、開發(fā)環(huán)境配置
5.ReactNative之Android打包APK方法(趟坑過程)
6Android 調(diào)試錯(cuò)誤總結(jié)(ReactNative開發(fā))(持續(xù)更新)
7.ReactNative開發(fā)之真機(jī)測(cè)試
8React Native開發(fā)之iOS打包ipa發(fā)布(親測(cè)可行)