【发布时间】:2020-05-02 07:25:20
【问题描述】:
如何在Alamofire 下载函数中创建协议(委托函数)来观察progress.fraction 然后我可以在任何viewController tableView 中使用它?
我的Alamofire 函数在主要公共类中,我必须检索文件下载的进度值。
Alamofire 下载功能
@objc public func downloadFile(url:String, filetype: String, callback:@escaping (_ success:Bool, _ result:Any?)->(Bool)) -> Void {
var destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
if filetype.elementsEqual(".pdf"){
destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadFileName = url.filName()
let fileURL = documentsURL.appendingPathComponent("\(downloadFileName).pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
}
Alamofire.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
//progress closure
print(progress.fractionCompleted)
}).response(completionHandler: { (DefaultDownloadResponse) in
callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.absoluteString.replacingOccurrences(of: "file://", with: ""))
print(DefaultDownloadResponse)
})
}
【问题讨论】:
标签: swift uitableview uiviewcontroller alamofire uiprogressview