官方文档,内容很旧:http://facebook.github.io/react-native/docs/running-on-device-ios.html#contenthtml
相关连接:https://github.com/facebook/react-native/issues/4084node
离线包就是把RN和你写的js图片等资源都打包放入app,不须要走网络下载。react
react-native bundleandroid
Options:ios
--entry-file Path to the root JS file, either absolute or relative to JS root [required]git
--platform Either "ios" or "android"github
--transformer Specify a custom transformer to be used (absolute path) [default: "/Users/babytree-mbp13/projects/xcodeProjects/AwesomeProject/node_modules/react-native/packager/transformer.js"]chrome
--dev If false, warnings are disabled and the bundle is minified [default: true]json
--prepack If true, the output bundle will use the Prepack format. [default: false]react-native
--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). [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]
在工程根目录下执行打包命令,好比react-native bundle --entry-file demo/index.js --bundle-output ./ios/bundle/index.ios.jsbundle --platform ios --assets-dest ./ios/bundle --dev false,请参考上面命令说明,根据本身的状况进行修改再执行。注意要先保证bundle文件夹存在。
命令执行完生成以下资源
在xcode中添加assets【必须用Create folder references的方式,添加完是蓝色文件夹图标】和index.ios.jsbundle,如图
参考官方文档,修改AppDelegate.m文件,使用OPTION 2处的代码
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index.ios" withExtension:@"jsbundle"];
一切OK 运行模拟器看效果吧
离线包若是开启了chrome调试,会访问调试服务器,并且会一直loading出不来。
若是bundle的名字是main.jsbundle,app会一直读取旧的,更名就行了。。。很是奇葩的问题,我从新删了app,clean工程都没用,就是不能用main.jsbundle这个名字。
必须用Create folder references【蓝色文件夹图标】的方式引入图片的assets,不然引用不到图片
执行bundle命令以前,要保证相关的文件夹都存在
For iOS open your project in Xcode and select Product → Scheme → Edit Scheme... (or press ⌘ + <). Next, select Run from the menu on the left and change the Build Configuration to Release.
For Android, by default, developer menu will be disabled in release builds done by gradle (e.g with gradle assembleRelease task). Although this behavior can be customized by passing proper value to ReactInstanceManager#setUseDeveloperSupport.