github上關(guān)于下載文件文檔如下:
// Alamofire 3
let destination = Request.suggestedDownloadDestination()
Alamofire.download(.GET, urlString, destination: destination).response { request, response, data, error in
// What is fileURL...not easy to get
print(request)
print(response)
print(data)
print(error)
}
// Alamofire 4
let destination = Request.suggestedDownloadDestination()
Alamofire.download(urlString, to: destination).response { response in // method defaults to `.get`
print(response.request)
print(response.response)
print(response.temporaryURL)
print(response.destinationURL)
print(response.error)
}
使用時發(fā)現(xiàn)urlString不是String類型枫绅,也不是URL類型,而是URLRequest類型,定義如下:
let urlString = URLRequest(url: URL(string:downloadfileURL)!)
第二個參數(shù)destination在Request類下并不存在,通過源代碼看出其在DownRequest類下,DownRequest繼承Request類蹬竖。
let filename : String = URL(string:downloadfileURL)!.lastPathComponent+".bin"
let fileUrl = directoryURL.appendingPathComponent(filename)
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
return (fileUrl, [.createIntermediateDirectories, .removePreviousFile])
}
下載方法如下:
Alamofire.download(urlString, to: destination).response { response in // method defaults to `.get`
}
終于搞定下載了,吐槽一下,用swift太坑了币厕,swift3一升級幾乎所有代碼都要改一遍列另,心累。