I am building an OS X Today Widget with Swift. This should call a JSON API via HTTPS and display the result. So far so good.
But when I run the widget in the Widget Simulator I always get the error "A server with the specified hostname could not be found". The URL is correct, it works when I do a curl on the command line. It seems that the Widget Simulator does no propper DNS Lookup or blocks https traffic completely.
I am using Alamofire but I also tried it with NSURLSession. Same result.
let url = NSURL(string: "https://httpbin.org/get")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
debugPrint(error)
}
task.resume()
Resulting in:
Optional(
Error
Domain=NSURLErrorDomain
Code=-1003 "A server with the specified hostname could not be found."
UserInfo={
NSUnderlyingError=0x7fe1f3e341a0
ErrorDomain=kCFErrorDomainCFNetwork
Code=-1003 "(null)"
UserInfo={
_kCFStreamErrorCodeKey=-72000,
_kCFStreamErrorDomainKey=10}
},
NSErrorFailingURLStringKey=https://httpbin.org/get,
NSErrorFailingURLKey=https://httpbin.org/get,
_kCFStreamErrorDomainKey=10,
_kCFStreamErrorCodeKey=-72000,
NSLocalizedDescription=A server with the specified hostname could not be found.
}
)
Widget Simulator should be able to do outside connections, is it?
This is a really old question, so pretty sure you had your answer already, but I ran into what I think is the same problem earlier and it took me an embarrassingly long time to figure out. Hopefully this will help someone else solve it quicker.
You can connect to https with the widget simulator. My version was Version 1.0 (552.6). The error I got was: 2017-01-07 14:35:26.062370 MYAPP [18387:5719297] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:5 Err:-1 Errno:1 Operation not permitted
Turns out you have to turn ON App Sandbox and explicitly allow network in and out. I guess I should had rad the documentation: https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW3
An extension has its own entitlements so make sure to configure each target that needs network.
Turning ON Sandbox allows network connection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With