http APi 请求连接失败错误1004
http APi request failed to connect error 1004
我在 Xcode 和 Swift 中使用 URLSession 来自自定义 Api 的 http 请求有问题。这是我的代码:
let forecast = self.forecastList[forecastIndex]
let url : URL = URL(string: "https:laundryireland.tk/getForecast?pwd=\(self.CustomApiKey)&temp=\(forecast.temp)&hum=\(forecast.humidity)&pres=\(forecast.pressure)&weat=\(forecast.weather)&wind=\(forecast.windspeed)")!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
do {
let scores = try JSONDecoder().decode(Scores.self, from: data)
// ...
} catch {
print("ops")
}
}
task.resume()
此代码已在第三行抛出错误:
2019-11-04 12:53:26.701212+0000 DIYP[2554:1119528] [] tcp_input [C6.1:3] flags=[R.] seq=0, ack=2949324956, win=0 state=SYN_SENT rcv_nxt=0, snd_una=2949324955
2019-11-04 12:53:26.702757+0000 DIYP[2554:1119528] Connection 6: received failure notification
2019-11-04 12:53:26.702843+0000 DIYP[2554:1119528] Connection 6: failed to connect 1:61, reason -1
2019-11-04 12:53:26.702897+0000 DIYP[2554:1119528] Connection 6: encountered error(1:61)
2019-11-04 12:53:26.706142+0000 DIYP[2554:1119528] Task <46A61430-630E-48F8-B121-82B1C7BEE5DF>.<2> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
我在此处看到: that the error 1004
is something related to the device not able to resolve the host but this seems odd to me because my URL is on a registered domain running on a remote server which is accessible from everywhere (just type: http://laundryireland.tk 在您的浏览器中看到它正在运行)。
我该如何解决这个问题?
所以这个问题首先是一个拼写错误:我的域名是 http://laundryireland.tk while in the code I wrote https://laundryireland.tk 。
我发现仍然可以更改应用程序设置以允许 http
连接:右键单击 Info.plist
、Open As > Source Code
,然后在顶部的 <dict>
之后复制粘贴以下代码:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我在 Xcode 和 Swift 中使用 URLSession 来自自定义 Api 的 http 请求有问题。这是我的代码:
let forecast = self.forecastList[forecastIndex]
let url : URL = URL(string: "https:laundryireland.tk/getForecast?pwd=\(self.CustomApiKey)&temp=\(forecast.temp)&hum=\(forecast.humidity)&pres=\(forecast.pressure)&weat=\(forecast.weather)&wind=\(forecast.windspeed)")!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
do {
let scores = try JSONDecoder().decode(Scores.self, from: data)
// ...
} catch {
print("ops")
}
}
task.resume()
此代码已在第三行抛出错误:
2019-11-04 12:53:26.701212+0000 DIYP[2554:1119528] [] tcp_input [C6.1:3] flags=[R.] seq=0, ack=2949324956, win=0 state=SYN_SENT rcv_nxt=0, snd_una=2949324955
2019-11-04 12:53:26.702757+0000 DIYP[2554:1119528] Connection 6: received failure notification
2019-11-04 12:53:26.702843+0000 DIYP[2554:1119528] Connection 6: failed to connect 1:61, reason -1
2019-11-04 12:53:26.702897+0000 DIYP[2554:1119528] Connection 6: encountered error(1:61)
2019-11-04 12:53:26.706142+0000 DIYP[2554:1119528] Task <46A61430-630E-48F8-B121-82B1C7BEE5DF>.<2> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
我在此处看到:1004
is something related to the device not able to resolve the host but this seems odd to me because my URL is on a registered domain running on a remote server which is accessible from everywhere (just type: http://laundryireland.tk 在您的浏览器中看到它正在运行)。
我该如何解决这个问题?
所以这个问题首先是一个拼写错误:我的域名是 http://laundryireland.tk while in the code I wrote https://laundryireland.tk 。
我发现仍然可以更改应用程序设置以允许 http
连接:右键单击 Info.plist
、Open As > Source Code
,然后在顶部的 <dict>
之后复制粘贴以下代码:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>