Mac平臺搭建React Native iOS開發環境
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
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” 報錯
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
解決參考: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開啟 點選執行按鈕,編譯執行成功。