iosxcodereact-nativeapp-transport-securitynsapptransportsecurity

react native networking not working after updating xcode to 8.3.2


all of a sudden, all my applications are getting Newtwork request failed after i updated my xcode to 8.3.2,

Network request failed
TypeError: Network request failed

i tried changing values in /ios/[projectname]/info.plist but still getting same error, even when i'm calling https:// endpoints.

<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>

steps to reproduce:- $ react-native init newProject && cd newProject && react-native run-ios

open index.ios.js and add test

            <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);
            }}/>

by clicking on both links they both will fail with newtwok request failed.

i tried using 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);
                    }}/>
}

yet exactly same error.

how can we fix this ??? i already finished my whole app and i'm waitting to fix this so i can publish :(

thanks


Solution

  • Your exception only allows for non-secure connections to the non-localhost url. You would need to change it to try with the http protocol (e.g. "http://ivf.simpleinfroamtics.com/system/settings"). If you want to connect via https, you would need to find out what about the URL is violating the ATS policies. It could be it doesn't support TLS 1.2, or that it doesn't support forward secrecy, or the key is insufficient.

    To test the URL for ATS compliance, use the nscurl --ats-diagnostics <url> command on your Mac. You can find out more about ATS in general, as well is how to use / interpret the results of the nscurl command above in this post.

    Finally, to allow the localhost connection, you need to add the NSAllowsLocalNetworking key with a value of true to allow your app to connect insecurely to local networking resources, including localhost.

    I would also recommend bumping up your CFNETWORK_DIAGNOSTICS level up so you can see exactly what is failing. You can find out more about that here.