好久没写简书了 去年我接触了react native 发现它很方便快速的完成双平台开发,
这里我先简单说下在做rn中遇到的一些问题 之后会陆续写更多
接手过一个老版本rn项目 当时是打包传过来的 当运行时发现了问题
首先是每次Xcode运行都会提示我
这是说你的launchPackager.command没有执行权限
然后会报
类似这个错误
解决方法是
在终端 输入 $chmod 755 /Users/....../node_modules/react-native/packager/launchPackager.command 和 $ chmod 755 /Users/....../node_modules/react-native/local-cli/setup_env.sh
即可 (你的本机路径)
另外 rn有时候会报一些你想不到的问题
按如下:
安装 npm-check-updates 这个包:
npm i -g npm-check-updates在项目目录下
rm -rf node_modules
ncu -u
npm install
这样可以解决很多莫名其妙的 node 包依赖问题
有时你用Xcode跑工程时 可能会出现这个问题
Script-00DD1BFF1BD5951E006B06BC.sh: line 3: ../node_modules/react-native/packager/react-native-xcode.sh: No such file or directory
这是因为react-native 低版本升级到0.49以上版本导致的
解决方法
1.打开 Xcode Build Phases > Bundle React Native code and images
2.修改export NODE_BINARY=node ../node_modules/react-native/packager/react-native-xcode.sh 为export NODE_BINARY=node ../node_modules/react-native/scripts/react-native-xcode.sh
老版本rn iOS工程里会这样生成代码
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index.ios" withExtension:@"jsbundle"];
这样如果后期 UI发生变化时 ios是不会跟着变得
按新版本的写 应该这样写
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];