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

标签 swift xcode macos https urlsession

我有这段代码:

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/

相关文章:

python - sudo pip install pil 在 Mac 10.9.1 上出错

ios - 游戏中心群组

swift - 想要在新行中打印数据

iphone - iOS 中涉及屏幕抓取的 Objective-C 和 Cocoa Touch 问题

Swift:具有字典属性的可哈希结构

swift - 单击按钮将移动到下一个 Controller ?

iOS Xcode Swift - 如何获取热点连接设备的 IP 地址?

java - 类未找到异常 : Didn't find class "com.xxx.xxx.Application" on path: DexPathList on Linux

swift - '|'运算符(operator)为我提供不同类型的结果

swift - 在Xcode 11.3 ViewController中崩溃