React Native在開發過程中遇到的一些問題(俗稱:坑)
阿新 • • 發佈:2019-01-27
伺服器地址錯誤
執行時產生以下錯誤:Could not connect to development server.
1、URL地址設定
問題:
Could not connect to development server. Ensure the following: - Node server is running and available on the same network - run 'npm start' from react-native root - Node server URL is correctly set in AppDelegate URL: http://localhost:8081/index.ios.bundle?platform=ios&dev=true 2016-08-29 17:52:29.307 [error][tid:main][RCTWebSocketManager.m:84] WebSocket connection failed with error Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused"
解決:
在AppDelegate.m
檔案中,把一下程式碼中的localhost
改為你電腦的IP地址,並且保證連線的是同一個網路(例如:連線同一個路由的網路)。
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
2、HTTP設定
2016-08-29 18:01:20.273 AwesomeProject[688:263429] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. 2016-08-29 18:01:20.316 [fatal][tid:main] Could not connect to development server. Ensure the following: - Node server is running and available on the same network - run 'npm start' from react-native root - Node server URL is correctly set in AppDelegate URL: http://10.240.139.46:8081/index.ios.bundle?platform=ios&dev=true
開啟ATS即可
提示
搖晃裝置來開啟開發選單(重新載入、除錯,等等……),這跟模擬器下的命令類似(Cmd + R to reload,Cmd + D)
watchman無法啟動的問題
現象:
watchman--no-pretty get-sockname returned with exit code 1
這個問題不影響app的執行,但是有紅色的Error總感覺不好
解決方法:
修改許可權
sudo chown -R `whoami` /usr/local
重新安裝watchman
brew link pcre
brew install –HEAD watchman
brew uninstall watchman
這時可能還是會遇到以下的錯誤
把錯誤提示的那些/usr/local下的檔案先刪除,然後執行"brew update"
然後安裝watchman: "brew install -HEAD watchman"
出現不能"symlink"的錯誤,按照提示執行:
這樣執行後,watchman就可能正常使用了
reactNative呼叫原生方法的問題
按照react-native中文網上的教程,學習iOS下的原生模組(iOS 日曆模組演示), 功能就是JS呼叫Object-C定義的方法,程式碼都是照抄,但是執行的時候出現以下錯誤,一直沒找到解決辦法。
後來發現OC程式碼的修改,如果要看效果,必須是通過Xcode執行,才能呼叫到原生方法,如果是在Deco中執行,就會出現上面的錯誤,官方文件並沒有給出提示,增加大家學習的難度。