ios - URLSession.dataTask 对 Xcode 12.5 Beta 2 的每个请求都超时

标签 ios swift xcode urlsession

我今天升级到最新的 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/

相关文章:

ios - 如何使 "Trust this developer"批准无缝地从 Xcode 侧载?

ios - 在 Xcode 中使用信号量进行调试 - Grand Central Dispatch - iOS

ios - 如果在 viewcontroller 中使用 UITabbar,如何设置 homeview controller

ios - 将 ImageBackground 调整为全屏

swift - tableview cell中水平collectionview的滚动问题

xcode - Parse.com 入门项目在 xcode 6.3 上出现 Cannot invoke 'subscribeToChannelInBackground' 编译器错误

swift - 为什么我不能在 Swift 的 for 循环中使用 i++

ios - 如何知道我的 iOS 应用程序是否具有正确的 Bundle ID?

ios - 如何在此 Segue 中添加基本 URL 和其他参数

ios - libobjc dealloc 中的 KERN_PROTECTION_FAILURE(堆栈溢出)