0

I would like to get data from my server using the IPAdress. I did :

    let url = NSURL(string: "http://51.255.XX.XXX/")

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
        (data, response, error) in

        if error == nil {

            print(data)
        }
        else {
            print("error")
        }

and my output was :

 App Transport Security has blocked a cleartext HTTP (http://) 
 resource load since it is insecure. 
 Temporary exceptions can be configured via your app's Info.plist file.

I checked this error on internet and I did modify my info.plist like here : App Transport Security has blocked a cleartext HTTP resource. So I added the IP Adress as an exception like below :

<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>http://51.255.XX.XXX/</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>

My code works fine if I replace the IP Adress with a fully domain name (http://www.domain.com) but it doesn't when I put the server's IP Address instead of domain.com

My question is, is it possible to get data with NSURL using IP Adress ? If yes, do you have documentations or example to give me ?

Community
  • 1
  • 1
Jibeee
  • 822
  • 1
  • 11
  • 26
  • are you *sure* your server's IP address is going to stay the same forever? It's usually a lot safer to use a fully qualified domain name (so the underlying IP address can be changed whenever necessary). – Michael Dautermann Dec 01 '15 at 13:42
  • Also, please show what exception you added to your app's Info.plist file (after all, there are a few ways to do app transport exceptions as detailed in that related question you linked to). – Michael Dautermann Dec 01 '15 at 13:44
  • @MichaelDautermann thank you for your quick answer. It is just for testing purpose for the moment. May the problem be linked with the dynamical change of IP Address ? – Jibeee Dec 01 '15 at 13:44
  • According to http://stackoverflow.com/questions/30903923/app-transport-security-and-ip-addresses-in-ios9, numeric IP addresses in the exception list just don't work. – Martin R Dec 01 '15 at 13:48
  • @MartinR that's why it doesn't work :) Thank you for your comment – Jibeee Dec 01 '15 at 13:51
  • You are welcome. (Actually it was the first Google hit for "app transport security ip address", it took me less than a minute to find a duplicate question :) – Martin R Dec 01 '15 at 13:52

0 Answers0