我最近将iPhone更新为iOS 10.3.1,将Xcode更新为8.3.2版。目前,我有一个应用程序,一旦我的应用程序启动,它会运行几个URLRequest
。
但是,在更新iOS和Xcode之后,应用程序偶尔会返回错误消息:
connection_block_invoke_2中的错误:连接中断
该消息非常模糊,但我认为它与URLRequest
有关,因为它提到“连接中断”。
每当出现此错误消息时,它会在从服务器获取数据之前将应用程序“冻结”~5秒。
那么,这个错误信息实际意味着什么?我该如何最小化或修复它?
URLRequest
的示例:func checkLogin () {
let username = txtUsername.text!
let password = txtPassword.text!
let post = "username=\(username)&password=\(password)"
NSLog("PostData: %@",post);
let url:URL = URL(string:"https://example.com/login.php")!
let postData = post.data(using: .utf8)!
let postLength = String( postData.count )
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
return
}
if let jsonData = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String:Any] {
let success = jsonData["success"] as! Int
if success == 1 {
//do something,
}
else {
//show alert
}
}
})
task.resume()
}
以下问题 解决了我的问题:
What is "error in __connection_block_invoke_2: Connection interrupted" in iOS?
iOS: error in __connection_block_invoke_2: Connection interrupted
AVCaptureMovieFileOutput
和AVCaptureVideoDataOutput
,因此答案也不适用。error in __connection_block_invoke_2: Connection interrupted on iPad& Xcode 7 error message "error in __connection_block_invoke_2: Connection interrupted" when app runs on iPad