React Native应用如何想访问HTTP资源应该怎么办

366 阅读1分钟

当我使用JavaScript fetch API访问http资源时,遇到一个错误:

clipboard1

Network Request Failed。

Google了一下,在stackoverflow上发现其他网友也遇到类似问题:

stackoverflow.com/questions/3…

clipboard2

网友回复说iOS默认不允许HTTP请求,只允许HTTPS。如果想用HTTP,需要在info.plist里加入下面信息:


<key>NSAppTransportSecurity</key>

<dict>

  <key>NSAllowsArbitraryLoads</key>

  <true/>

</dict>

<key>NSAppTransportSecurity</key>

<dict>

 <key>NSExceptionDomains</key>

 <dict>

  <key>yourserver.com</key>

  <dict>

   <!--Include to allow subdomains-->

   <key>NSIncludesSubdomains</key>

   <true/>

   <!--Include to allow HTTP requests-->

   <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>

   <true/>

   <!--Include to specify minimum TLS version-->

   <key>NSTemporaryExceptionMinimumTLSVersion</key>

   <string>TLSv1.1</string>

  </dict>

 </dict>

</dict>

详细信息参考React Native的官网:

facebook.github.io/react-nativ…

要获取更多Jerry的原创文章,请关注公众号"汪子熙":