博客 / 詳情

返回

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

問題

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

原因

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

解決方案

  1. xcode中打開react native中的ios項目根目錄;
  2. 打開項目下的info.plist文件;
    image.png
  3. App Transport Security Settings下新增Allow Arbitrary Loads,值為YES;
    image.png
  4. 重啓react native項目,刷新,請求後返回值正常;
user avatar jidongdehai_co4lxh 頭像 yaofly 頭像 esunr 頭像 iymxpc3k 頭像 huanjinliu 頭像 musicfe 頭像 light_5cfbb652e97ce 頭像 user_ze46ouik 頭像 caideheirenyagao 頭像 u_12219 頭像 xiaojt 頭像 lilin_5e390e08b42e4 頭像
15 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.