Seven, use Alamofire for file download
1, custom download File save directory
The following code downloads the logo picture to the user's document directory (Documnets directory), and the filename is unchanged.
Alamofire.download (. Get, "Yun_qi_img/logo.png") {
Temporaryurl, response in
Let FileManager = Nsfilemanager.defaultmanager ()
Let Directoryurl = Filemanager.urlsfordirectory (. Documentdirectory,
Indomains:. Userdomainmask) [0]
Let pathcomponent = Response.suggestedfilename
Return Directoryurl.urlbyappendingpathcomponent (pathcomponent!)
}
2, using the download path provided by default
The Alamofire built-in many popular download paths are convenient for us to use and simplify the code. For example, download to the user documentation directory can be changed to:
Let Destination = Alamofire.Request.suggestedDownloadDestination (
Directory:. Documentdirectory, domain:. Userdomainmask)
Alamofire.download (. Get, "Yun_qi_img/logo.png",
Destination:destination)
3, download progress
The following code prints the download progress continuously during the file download, and the completion information is also printed when the download is complete.
Let Destination = Alamofire.Request.suggestedDownloadDestination (
directory:. Documentdirectory, domain:. Userdomainmask)
Alamofire.download (. Get, "Http://www.hangge.com/favicon.ico", destination:destination)
Progress {(Bytesread, Totalbytesread, Totalbytesexpectedtoread) in
let percent = Totalbytesread*100/totalbytesexpectedtoread
Print (downloaded: \ ( Totalbytesread) Current progress: \ (percent)% ")
}
. Response {(Request, Response, _, error) in
print (response)
}
4, the breakpoint continues to pass (Resume Data)
When the download is stopped unexpectedly, you can save the downloaded part in the response method and continue downloading from the breakpoint the next time.
The following example demonstrates how to continue a breakpoint:
(1) Automatically start downloading files after the program starts
(2) Click "Stop Download", terminate the download and save the downloaded data, progress bar to stop walking.
(3) Click "Continue to Download", from the last termination of the place to continue downloading, progress bar continue to walk.
Original: Swift-http network operation library Alamofire use detailed 3 (file download, breakpoint continued transmission) Original: Swift-http network operation library Alamofire use details 3 (file download, breakpoint continued transmission)
Import Uikit
Import Alamofire
Class Viewcontroller:uiviewcontroller {
Stop Download button
@IBOutlet weak var stopbtn:uibutton!
Continue downloading button
@IBOutlet weak var continuebtn:uibutton!
Download progress bar
@IBOutlet weak var progress:uiprogressview!
Save path to download file
Let Destination = Alamofire.Request.suggestedDownloadDestination (
Directory:. Documentdirectory, domain:. Userdomainmask)
To save the downloaded part when stopping the download
var cancelleddata:nsdata?
Download Request Object
var downloadrequest:request!
Override Func Viewdidload () {
Super.viewdidload ()
Start downloading automatically when the page is loaded
Self.downloadrequest = Alamofire.download (. GET,
"Http://dldir1.qq.com/qqfile/qq/QQ7.9/16621/QQ7.9.exe",
Destination:destination)
Self.downloadRequest.progress (downloadprogress)//Download Progress
Self.downloadRequest.response (completionhandler:downloadresponse)//Download Stop response
}
Change progress bar during download
Func downloadprogress (Bytesread:int64, Totalbytesread:int64,
Totalbytesexpectedtoread:int64) {
Let percent = Float (totalbytesread)/float (Totalbytesexpectedtoread)
Progress bar Update
Dispatch_async (Dispatch_get_main_queue (), {
Self.progress.setProgress (Percent,animated:true)
})
Print ("Current progress: \ (percent*100)%")
}
Download stops responding (regardless of success or failure)
Func downloadresponse (request:nsurlrequest, Response:nshttpurlresponse?
Data:nsdata, Error:nserror? {
If let error = error {
if Error.code = = nsurlerrorcancelled {
Self.cancelleddata = data///accidental termination, save downloaded information
} else {
Print ("Failed to download file: \ (response) \ (Error)")
}
} else {
Print ("Successfully downloaded file: \ (response)")
}
}
Stop button click
@IBAction func Stopbtnclick (sender:anyobject) {
Self.downloadrequest? Cancel ()
Self.stopBtn.enabled = False
Self.continueBtn.enabled = True
}
Continue button click
@IBAction func Continuebtnclick (sender:anyobject) {
If let Cancelleddata = self.cancelleddata {
Self.downloadrequest = Alamofire.download (Resumedata:cancelleddata,
Destination:destination)
Self.downloadRequest.progress (downloadprogress)//Download Progress
Self.downloadRequest.response (completionhandler:downloadresponse)//Download Stop response
Self.stopBtn.enabled = True
Self.continueBtn.enabled = False
}
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}