-2

I've a problem. Safari can load the page http://ip-api.com/json, but a URLSession task cannot. Here's the code inside viewDidLoad

    let url = URL(string: "http://ip-api.com/json")!
    let request = URLRequest(url: url)
    let task = URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
        print("this is the server response: \(response)")
        if error != nil {
            print("some error: \(error)")
        } else {
            if let urlContent = data {
                print(urlContent)
            }
        }
    })
    task.resume()

The console log reports the following error content:

... some error: Optional(Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x60800005bc00 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://ip-api.com/json, NSErrorFailingURLKey=http://ip-api.com/json, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.})

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • 1
    Possible duplicate of [The resource could not be loaded because the App Transport Security policy requires the use of a secure connection](http://stackoverflow.com/questions/32631184/the-resource-could-not-be-loaded-because-the-app-transport-security-policy-requi) – vadian Oct 14 '16 at 10:09

2 Answers2

0

There was a trivial mistake! When editing the plist item I left a space between <key> and the property name, as follows <key> NSAppTransportSecurity</key>. Sorry

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
0

In your info.plist file add this,

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict/>
        <key> http://ip-api.com</key>
        <string></string>
    </dict>

It will work for you.

KAR
  • 3,303
  • 3
  • 27
  • 50