When I test my project in the condition of no internet connection, for calling upload function, I can't catch failure situation.
I have printed the status code but I am getting nil and shows my default value.
What's wrong with me?
And I also got this error like following text:
==)[r]: FAILURE: Error Domain=NSURLErrorDomain Code=-1009 "似乎已断开与互联网的连接。" UserInfo={NSUnderlyingError=0x1d4441560 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=https://appdominname/chatroom/5a3cba41f7255b356eeee33/media, NSErrorFailingURLKey=https://appdominname/chatroom/5a3cba41f7255b356eeee33/media, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=似乎已断开与互联网的连接。}
==)[c]: 123
func uploadPost(parameters:[String:Any], fileUrl:URL, _ callback: @escaping (JSON) -> Void) {
let url = self.customUrl(parameters)
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(fileUrl, withName: "media")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, usingThreshold: .init(), to: url, method: .post, headers: ["Authorization": "Bearer \(currentUser.token)"]) { (encodingResult) in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON(completionHandler: { (response) in
print("==)[r]:",response)
print("==)[c]:",response.response?.statusCode ?? "123")
if let statusCode = response.response?.statusCode {
switch statusCode {
case 201:
print("success statusCode: \(statusCode)")
default:
print("error with response status: \(statusCode)")
}
}
if let result = response.result.value {
let json = result as! NSDictionary
// print(json["status"] ?? "")
callback(JSON(json))
}
})
case .failure(let encodingError):
alertWithConfirm(title: localString(string: "WARNING"), message: localString(string: "ERROR_UPLOAD_API_FAIL"), afterConfirm: { _ in
callback(JSON(encodingError))
})
print("error:\(encodingError)")
}
}
}