如何在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)
})
}
类似这样的东西:
protocol MyWebServiceProtocol: class {
func progress(_ fractionCompleted: Int)
func downloadDidSucceed(data: Data)
func downloadDidFail(error: Error)
}
class MyViewController: UIViewController {
let webService: MyWebService
init(webService: MyWebService = MyWebService()) {
self.webService = webService
super.init(nibName: nil, bundle: nil)
self.webService.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension MyViewController: MyWebServiceProtocol {
func progress(_ fractionCompleted: Int) {
print(fractionCompleted) // do something with progress
}
func downloadDidSucceed(data: Data) {
// Do something here
}
func downloadDidFail(error: Error) {
// handle error
}
}
class MyWebService {
weak var delegate: MyWebServiceProtocol?
@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: { [weak self] (progress) in
//progress closure
self?.delegate?.progress(progress.fractionCompleted)
print(progress.fractionCompleted)
}).response(completionHandler: { [weak self] (DefaultDownloadResponse) in
callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.absoluteString.replacingOccurrences(of: "file://", with: ""))
self?.delegate?.downloadDidSucceed()
print(DefaultDownloadResponse)
})
}