React Native mac環境下解決iOS 9以上系統對HTTP請求的限制

問題

新搭建的一個項目,在使用fetch進行網絡請求的時候遇到一個報錯:
TypeError: Network request failed.
嘗試直接在瀏覽器打開url訪問能夠正常拿到返回值。(url是http協議)react

緣由

這個API請求是http的協議請求,在iOS9以上系統,已經把http協議的請求限制了。
iOS9引入了新特性App Transport Security (ATS)。詳情:App Transport Security (ATS)
新特性要求App內訪問的網絡必須使用HTTPS協議。ios

解決方案

  1. xcode中打開react native中的ios項目根目錄;
  2. 打開項目下的info.plist文件;
    image.png
  3. App Transport Security Settings下新增Allow Arbitrary Loads,值爲YES;
    image.png
  4. 重啓react native項目,刷新,請求後返回值正常;
相關文章
相關標籤/搜索