URLSession "hostname not found", but I see it in browser

Hi


I have this code:


      let primayCurr = "https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes"
   
      guard let url = URL(string: primayCurr) else { print("Invalid URL: \(primayCurr)"); return nil
      let task = URLSession.shared.dataTask(with: url) { data, response, error in
            guard error == nil else {
                print(error!)
                return
            }
            guard let data = data else {
                print("Data is empty")
                return
            }           
            let json = try! JSONSerialization.jsonObject(with: data, options: [])
            print("JSON: ", json)
      }
      task.resume()


The message area gets this:

2017-11-21 22:41:58.919318+1000 AltcoinTrader[36412:12415444] dnssd_clientstub ConnectToServer: connect()-> No of tries: 1

2017-11-21 22:41:59.921886+1000 AltcoinTrader[36412:12415444] dnssd_clientstub ConnectToServer: connect()-> No of tries: 2

2017-11-21 22:42:00.922257+1000 AltcoinTrader[36412:12415444] dnssd_clientstub ConnectToServer: connect()-> No of tries: 3

2017-11-21 22:42:01.927400+1000 AltcoinTrader[36412:12415444] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:11 Err:-1 Errno:1 Operation not permitted

2017-11-21 22:42:01.927796+1000 AltcoinTrader[36412:12415444] [] nw_resolver_create_dns_service_locked DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563)

2017-11-21 22:42:01.928105+1000 AltcoinTrader[36412:12415444] TIC TCP Conn Failed [1:0x608000162580]: 10:-72000 Err(-65563)

2017-11-21 22:42:01.929003+1000 AltcoinTrader[36412:12415423] Task <209F1681-53BB-4E9D-A553-E349AE087159>.<1> HTTP load failed (error code: -1003 [10:-72000])

2017-11-21 22:42:01.929234+1000 AltcoinTrader[36412:12415423] Task <209F1681-53BB-4E9D-A553-E349AE087159>.<1> finished with error - code: -1003

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x604000046720 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey=https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes/, NSErrorFailingURLKey=https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes/, _kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not be found.}



Not sure if the -1003 code is the more relevant one. I was getting that when trying

String(contentsOf: url)

I know the URL is a real one because I can paste it into a browser and get the right JSON string back.


What am I doing wrong here? I am doing a mac app in Xcode 9.1 on an up-to-date macOS.


Regards

Mark

I fixed this by changing the permissions in the Sandbox.


Specifically, in Project navigator I selected the project, then in the main window selected the app, then the Capabilities tab. App Sandbox was switched on, but all the items were unchecked. I turned on both the Network ones (Incoming and outgoing).

Then it worked 😀

URLSession "hostname not found", but I see it in browser
 
 
Q