我在使用Swift 3时遇到了一个奇怪的错误。
我正在尝试在Swift应用程序中请求一个URL,但请求总是失败。我尝试在浏览器中打开不同的网址并打开网址,请求可以使用。
我使用的是我在网上找到的以下代码段:
let url = URL(string: "https://httpbin.org/ip")
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)
}
task.resume()
这是我的程序产生的错误
2018-04-30 11:11:08.318299+0200 TokenList[2765:48745] PAC result block not invoked
2018-04-30 11:11:08.318361+0200 TokenList[2765:48745] [] nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNetwork: 308
2018-04-30 11:11:08.318380+0200 TokenList[2765:48737] Received XPC error Connection invalid for message type 3 kCFNetworkAgentXPCMessageTypePACQuery
2018-04-30 11:11:08.318880+0200 TokenList[2765:48745] dnssd_clientstub ConnectToServer: connect()-> No of tries: 1
2018-04-30 11:11:09.319675+0200 TokenList[2765:48745] dnssd_clientstub ConnectToServer: connect()-> No of tries: 2
2018-04-30 11:11:10.322336+0200 TokenList[2765:48745] dnssd_clientstub ConnectToServer: connect()-> No of tries: 3
2018-04-30 11:11:11.323967+0200 TokenList[2765:48745] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:10 Err:-1 Errno:1 Operation not permitted
2018-04-30 11:11:11.324569+0200 TokenList[2765:48745] [] nw_resolver_create_dns_service_locked DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563)
2018-04-30 11:11:11.325512+0200 TokenList[2765:48745] TIC TCP Conn Failed [1:0x6000001630c0]: 10:-72000 Err(-65563)
2018-04-30 11:11:11.326603+0200 TokenList[2765:48737] Task <F194834F-65B5-4E6D-B088-B4947A029818>.<1> HTTP load failed (error code: -1003 [10:-72000])
2018-04-30 11:11:11.326897+0200 TokenList[2765:48737] Task <F194834F-65B5-4E6D-B088-B4947A029818>.<1> finished with error - code: -1003
Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x600000442250 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey=https://httpbin.org/ip, NSErrorFailingURLKey=https://httpbin.org/ip, _kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not be found.}
我怀疑这个问题可能是因为我在eduroam,一个门卫严重的大学wifi。另一方面,其他请求(来自其他macOS)应用程序正常工作。
此外,Mac上的配置错误应该没有问题,因为它是全新的,只有自制软件和一些程序已安装。
我期待您的任何指示,并希望提前感谢您。