仅在网络类型为4g的iPhone 7上遇到 URLSessionDataTask
问题,网络类型为WiFi的设备运行相同。任何人都可以帮助我吗?
这是Swift 3中的代码:
func getParentAccount(onGetParentAccountComplete: OnGetParentAccountComplete) {
let url = URL(string: "http://...")!
let authorizationHeader = authorizationHeaderFactory.getOAuth2Authorization()
var request = URLRequest(url: url)
request.setValue(authorizationHeader["Authorization"], forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
// error
print("Error: \(String(describing: error))")
onGetParentAccountComplete.onGetParentAccountFailure(message: "Communication error")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 200, let json = try? JSONSerialization.jsonObject(with: data, options: []) {
// ok
let parentAccountDTO = self.parentAccountMapper.mapJSONToDTO(json: json as! [String : Any])
onGetParentAccountComplete.onGetParentAccountSuccess(parentAccount: self.parentAccountMapper.mapDomain(source: parentAccountDTO))
return
} else {
// error
print("Error: response = \(String(describing: response))")
onGetParentAccountComplete.onGetParentAccountFailure(message: "Error response")
return
}
}
task.resume()
}