您的位置:首页 > Web前端 > React

【React Native】iOS离线打包

2016-10-19 15:32 302 查看
我们可以用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)..) [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文件夹。

操作1:

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


准备就绪,开始打包。打包完后你就会看到main.js文件变为已有状态了。

  然后打开Appdelegate.m文件,修改:

  操作2:

  //jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@”index.ios” fallbackResource:nil];

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@”main” withExtension:@”jsbundle”];

操作3:

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



最后command+R运行,发现:



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

操作4:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios native