【问题标题】:react native networking not working after updating xcode to 8.3.2将 xcode 更新到 8.3.2 后,本机网络无法正常工作
【发布时间】:2017-10-20 22:56:06
【问题描述】:

突然间,在我将 xcode 更新到 8.3.2 后,我所有的应用程序都收到 Newtwork 请求失败,

Network request failed
TypeError: Network request failed

我尝试更改 /ios/[projectname]/info.plist 中的值,但仍然出现相同的错误,即使我正在调用 https:// 端点。

<key>NSAppTransportSecurity</key>
    <!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>

重现步骤:- $ react-native init newProject && cd newProject && react-native run-ios

打开 index.ios.js 并添加测试

            <Button title="fetch" onPress={async ()=>{
              const r = await fetch('http://localhost/ivf/system/settings');
              console.log(r);
            }}/>
            <Button title="fetchs" onPress={async ()=>{
              const r = await fetch('https://ivf.simpleinfroamtics.com/system/settings').catch(console.log);
              console.log(r);
            }}/>

通过点击这两个链接,它们都会失败,并且 newtwok 请求失败。

我尝试使用 xhr insteed

componentDidMount(){
                  <Button title="console" onPress={()=>{
                      console.log('log');
                      console.debug('debug');
                      console.info('info');
                      console.warn('warn');
                    }}/>
                    <Button title="fetch" onPress={async ()=>{
                      const r = await fetch('http://localhost/ivf/system/settings');
                      console.log(r);
                    }}/>
                    <Button title="fetchs" onPress={async ()=>{
                      const r = await fetch('https://ivf.simpleinfroamtics.com/system/settings').catch(console.log);
                      console.log(r);
                    }}/>
}

但完全相同的错误。

我们怎样才能解决这个问题???我已经完成了我的整个应用程序,我正在等待修复这个问题,以便我可以发布:(

谢谢

【问题讨论】:

    标签: ios xcode react-native app-transport-security nsapptransportsecurity


    【解决方案1】:

    您的例外仅允许与非本地主机 url 的非安全连接。您需要更改它以尝试使用 http 协议(例如“http://ivf.simpleinfroamtics.com/system/settings”)。如果您想通过 https 进行连接,则需要找出该 URL 违反 ATS 政策的原因。可能是它不支持 TLS 1.2,或者它不支持前向保密,或者密钥不足。

    要测试 URL 是否符合 ATS,请在 Mac 上使用 nscurl --ats-diagnostics &lt;url&gt; 命令。您可以在此post 中找到有关 ATS 的更多信息,以及如何使用/解释上述 nscurl 命令的结果。

    最后,要允许 localhost 连接,您需要添加值为 true 的 NSAllowsLocalNetworking 键,以允许您的应用不安全地连接到本地网络资源,包括 localhost。

    我还建议您提高您的 CFNETWORK_DIAGNOSTICS 级别,这样您就可以准确地看到失败的原因。您可以了解更多关于 here 的信息。

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 1970-01-01
      • 2021-01-16
      • 2011-11-23
      • 2019-07-04
      • 2023-03-12
      • 2021-10-24
      • 2013-05-28
      • 2019-08-17
      相关资源
      最近更新 更多