React Native ios打包

1.在自己项目的ios文件夹下新建一个文件夹取名bundle(ps:ios文件夹和node_modules文件夹在同一级目录下,这个bundle文件夹名称随意取,后面要用到,但是记得在相应地方改一下就好了)

2.为了方便,将打包命令写到项目package.json文件里,
然后执行命令:

npm run bundle-ios


打包命令为:

"bundle-ios":"node node_modules/react-native/local-cli/cli.js bundle --entry-file index.js --platform ios --dev false --bundle-output ./ios/bundle/index.jsbundle --assets-dest ./ios/bundle"

"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"bundle-ios":"node node_modules/react-native/local-cli/cli.js bundle --entry-file index.js --platform ios --dev false --bundle-output ./ios/bundle/index.jsbundle --assets-dest ./ios/bundle"
},

打包命令完成后会在我们新建的文件夹bundle里面生成相应的文件。

3.用Xcode打开项目,选中跟项目同名的那个文件夹,右键,选中‘Add Files to ..’选项,然后选择新建的那个bundle文件夹,在弹出的对话框中,点击options,一定要勾选Create folder references选项,将bundle文件夹添加到项目里,Xcode下该文件夹一定要是蓝色的。

4.修改AppDelegate.m中的加载包的方式(只需修改一次),之后项目会自动跟据debug还是release状态加载不同的包,我们不用在管了

NSURL *jsCodeLocation;
#ifdef DEBUG
     //开发包

    jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];


#else //离线包

    jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"bundle/index" withExtension:@"jsbundle"];

 #endif

5.将项目由debug状态改成release状态,Xcode-->Product-->Scheme-->Edit Scheme...
 
 
参考资料:https://www.jianshu.com/p/ce71b4a8a246

猜你喜欢

转载自www.cnblogs.com/cui-cui/p/9207443.html