天天看點

React Native在開發過程中遇到的一些問題(俗稱:坑)

伺服器位址錯誤

運作時産生以下錯誤:Could not connect to development server.

1、URL位址設定

問題:

[objc] ​​view plain​​ ​​copy​​

  1. Could not connect to development server.
  2. Ensure the following:
  3. - Node server is running and available on the same network - run 'npm start' from react-native root
  4. - Node server URL is correctly set in AppDelegate
  5. URL: http://localhost:8081/index.ios.bundle?platform=ios&dev=true
  6. 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位址,并且保證連接配接的是同一個網絡(例如:連接配接同一個路由的網絡)。

[objc] ​​view plain​​ ​​copy​​

  1. jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];

2、HTTP設定

[objc] ​​view plain​​ ​​copy​​

  1. 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.
  2. 2016-08-29 18:01:20.316 [fatal][tid:main] Could not connect to development server.
  3. Ensure the following:
  4. - Node server is running and available on the same network - run 'npm start' from react-native root
  5. - Node server URL is correctly set in AppDelegate
  6. URL: http://10.240.139.46:8081/index.ios.bundle?platform=ios&dev=true

開啟ATS即可

React Native在開發過程中遇到的一些問題(俗稱:坑)

提示

搖晃裝置來打開開發菜單(重新加載、調試,等等……),這跟模拟器下的指令類似(Cmd + R to reload,Cmd + D)

watchman無法啟動的問題

現象:

[javascript] ​​view plain​​ ​​copy​​

  1. watchman--no-pretty get-sockname returned with exit code 1
React Native在開發過程中遇到的一些問題(俗稱:坑)

這個問題不影響app的運作,但是有紅色的Error總感覺不好

解決方法:

修改權限

sudo chown -R `whoami` /usr/local

重新安裝watchman 

brew link pcre 

brew install –HEAD watchman

brew uninstall watchman 

這時可能還是會遇到以下的錯誤

把錯誤提示的那些/usr/local下的檔案先删除,然後執行"brew update"

React Native在開發過程中遇到的一些問題(俗稱:坑)

然後安裝watchman: "brew install -HEAD watchman"

React Native在開發過程中遇到的一些問題(俗稱:坑)

出現不能"symlink"的錯誤,按照提示執行:

React Native在開發過程中遇到的一些問題(俗稱:坑)

這樣執行後,watchman就可能正常使用了

reactNative調用原生方法的問題

按照react-native中文網上的教程,學習iOS下的原生子產品(iOS 月曆子產品示範), 功能就是JS調用Object-C定義的方法,代碼都是照抄,但是運作的時候出現以下錯誤,一直沒找到解決辦法。

React Native在開發過程中遇到的一些問題(俗稱:坑)

後來發現OC代碼的修改,如果要看效果,必須是通過Xcode運作,才能調用到原生方法,如果是在Deco中運作,就會出現上面的錯誤,官方文檔并沒有給出提示,增加大家學習的難度。

參考:

​​1. 在裝置上運作React Native項目​​

2. ​​React-Native Start的時候遇到的報錯​​