我有这种代码
let req = Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
req.downloadProgress { (progress) in
print("progess!", Float(req.progress.fractionCompleted))
}
req.responseJSON { response in
if response.result.isSuccess {
print(response.result.value!)
}
else {
print(response.result.error!)
}
}
结果并没有给我一个进展。它只是返回1.0
,就是这样,但我需要这个请求的进展。
请有人帮帮我
使用req.progress.fractionCompleted
时你犯了一个错误。改用progress.fractionCompleted
。
let req = Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
req.downloadProgress { (progress) in
print("progess!", Float(progress.fractionCompleted))
}
req.responseJSON { response in
if response.result.isSuccess {
print(response.result.value!)
}
else {
print(response.result.error!)
}
}
在主队列中添加进度
request?.downloadProgress(queue: .main,closure: {progress in
self.downloadProcess.text = "Download \((progress.fractionCompleted*100))% completed"
print(self.downloadProcess.text)
if #available(iOS 11.0, *) {
} else {
// Fallback on earlier versions
}
})