【React Native】iOS离线打包

来源:互联网 发布:十万块钱怎么理财 知乎 编辑:程序博客网 时间:2024/06/05 09:05

  我们可以用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文件,不过是红色状态的,因为引用了但是并没有这个文件存在,React Native这样做的目的可能就是提示你还没有生成离线数据的意思吧。
  所以我们这里的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文件变为已有状态了。
  然后打开Appdelegate.m文件,修改:

//jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];  jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

  然后再把assets文件夹添加到工程中,注意的是必须选择Creat folder references否则运行时将会报错,无法找到文件。


assets文件夹

  最后command+R运行,发现:


报错

  出现这样的原因是因为Build Phases中Copy Bundle Resources没有添加我们刚才生成的main.js。


Copy Bundle Resources

  再次command+R运行,跑起来了。

0 0
原创粉丝点击