1、安装node.js
下载node.js安装包进行安装 https://nodejs.org/zh-cn/
命令行查看Node js版本:npm –v,出现版本号说明安装成功
2、 安装React Native命令行工具
命令行下输入:sudonpm install -g react-native-cli
安装成功后,可以通过react-native –h来查看帮助
3、 安装ios开发工具xcode
在AppStore搜索安装xcode
4、 更换npm镜像为淘宝镜像
查看镜像源
npmconfig get registry
更换为taobao源
npmconfig set registry https://registry.npm.taobao.org --global
npmconfig set disturl https://npm.taobao.org/dist --global
更换为官方镜像源
npmconfig set registry https://registry.npmjs.org/
5、 初始化rn项目
react-native init FirstApp
6、 运行rn项目
切换到项目根目录FirstApp下,输入react-native run-ios运行,会自动对项目进行编译,启动一个默认的ios模拟器,并启动一个包管理服务器(为了向rn应用来同步代码)。
附:指定项目运行的模拟器或真机
需要安装软件
sudo npm install -g ios-deploy
报错了,用下面命令
sudo npm install -g ios-deploy --unsafe-perm=true
运行到真机
react-native run-ios --device “6s test iphone”
运行到模拟器
react-native run-ios --simulator “6s test iphone”
报错:No matching provisioning profiles found
还需要在xcode下设置登录信息
好了,真机下运行成功
7、 运行报错
Loading dependency graph, done.
error: bundling failed: Error: Unable to resolve module ./../react-transform-hmr/lib/index.js
from /Users/sidashidai/Desktop/rn/FirstApp/App.js
: The module ./../react-transform-hmr/lib/index.js
could not be found from /Users/sidashidai/Desktop/rn/FirstApp/App.js
. Indeed, none of these files exist:
在这里插入图片描述
解决参考:http://www.imooc.com/qadetail/289035
关掉你的cmd窗口(其他的内置命令行同理)
mac 下操作
# Clean cache
rm -rf $TMPDIR/react-;
rm -rf $TMPDIR/haste-;
rm -rf $TMPDIR/metro-*;
watchman watch-del-all(无效)
# Start Metro Bundler directly
react-native start
# 重新打开一个命令行窗口
react-native run-ios
window 下操作
# Clean cache
react-native start --reset-cache
# new cmd tab重新打开一个命令行窗口
react-native run-android
发现有其他坑的,建议去gayhub去找,还是那里最靠谱。
8、 运行成功,比window下环境搭建错误少
9、接下来,用xcode运行rn项目
进入项目根目录-ios-.xcodeproj,双击用Xcode打开
点击运行按钮,编译运行成功。