我今天升级到最新的 Xcode 12.5 Beta 2,现在我所有的 URLSession.dataTask 请求都失败并超时。我创建了一个示例项目,它提出了一个简单的请求,但每次都失败。它适用于 Xcode 12.5 Beta 1。
这是一个简单的请求:
guard let url = URL(string: "https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty") else { fatalError() }
let startTime = Date()
let task = URLSession.shared.dataTask(with: url) { data, response, error in
let requestTime = Date().timeIntervalSince(startTime)
print("Time for request: \(requestTime)")
if let error = error {
updateLabel("requestTime: \(requestTime)\nError: \(error.localizedDescription)")
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
updateLabel("requestTime: \(requestTime)\n\(response.debugDescription)")
return
}
if let mimeType = httpResponse.mimeType, mimeType == "text/html",
let data = data,
let string = String(data: data, encoding: .utf8) {
DispatchQueue.main.async {
print(string)
// self.webView.loadHTMLString(string, baseURL: url)
}
}
}
task.resume()
func updateLabel(_ text: String) {
print(text)
}
测试版中的其他人是否有同样的问题?
最佳答案
是的,我遇到了同样的问题,我正在努力解决它。您的代码似乎没有任何问题。可能是对xcode的一些许可。请务必在 info.plist enter image description here 中添加此行
关于ios - URLSession.dataTask 对 Xcode 12.5 Beta 2 的每个请求都超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66247934/