gpt4 book ai didi

swift - URLSession "hostname not found",但我在浏览器中看到它

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:53 26 4
gpt4 key购买 nike

我有这段代码:

let primaryCurr = "https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes"

guard let url = URL(string: primaryCurr) else {
print("Invalid URL: \(primaryCurr)")
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()
}

消息区得到这个:

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.}

-1003 代码可能是更相关的代码。我在尝试 String(contentsOf: url) 时得到了我知道该 URL 是有效的,因为我可以将其粘贴到浏览器中并返回正确的 JSON 字符串。我在这里做错了什么?我正在最新的 macOS 10.13.1 上的 Xcode 9.1 中做我的第一个 mac 应用程序。

问候

标记

最佳答案

我通过更改沙盒中的权限解决了这个问题。

具体来说,我在 Project navigator 中选择了项目,然后在主窗口中选择了应用程序,然后是 Capabilities 选项卡。 App Sandbox 已打开,但所有项目均未选中。我同时打开了网络(传入和传出)。

然后就成功了

我通过谷歌搜索找到了这个并最终看到了这个页面: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW9

关于swift - URLSession "hostname not found",但我在浏览器中看到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47425178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com