In the project, we will often encounter the need to download the program, such as downloading music online, download pictures and so on, today I will introduce the use of ASIHTTPRequest download example, support for the continuation of the breakpoint, the use of asihttprequest download and the principle of the continuation of the breakpoint in my blog: http ://www.cnblogs.com/webapplee/p/3784599.html has a specific introduction, today focuses on how to achieve, nonsense less say, the beginning of the text:
One, create a network request queue
First, create the network request queue, as follows:
Asinetworkqueue *que = [[Asinetworkqueue alloc] init];
Self.networkqueue = que;
[Que release];
[Self.networkqueue Reset];
[Self.networkqueue Setshowaccurateprogress:yes];
[Self.networkqueue go];
Second, create a storage path
Initialize Documents path
NSString *path = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];
// Initialize temporary file path
NSString *folderpath = [path stringbyappendingpathcomponent:@ "temp"];
// Create File Manager
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
// determine if the temp folder exists
BOOL fileexists = [FileManager Fileexistsatpath:folderpath];
if (!fileexists) {// if not present , create, because the folder is not created automatically when downloading
[FileManager Createdirectoryatpath:folderpath
Withintermediatedirectories:yes
Attributes:nil
Error:nil];
}
Third, send the download request
Here are some of the following objects:Customcell is my custom Cell,cell on the download and pause two buttons, whose tag value is the row of the cell, so here [Sendertag] is the download button's tag value, Self.downloadarray is an array object that holds the resource dictionary information to be downloaded, in which a key is a URL, and its corresponding value is our download link.
These things, according to their actual needs to change can be used
Customcell *cell = (Customcell *) [Self.mytableview cellforrowatindexpath:[nsindexpath indexPathForRow:[sender Tag] INSECTION:0]];
NSString *filepath = [[Self.downloadarray objectatindex:[sender tag]] objectforkey:@ "URL"];
NSLog (@ "filepath=%@", FilePath);
Initial Download Path
Nsurl *url = [Nsurl Urlwithstring:filepath];
// Set Download path
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initwithurl:url];
setting up the ASIHTTPRequest agent
Request.delegate = self;
Initialize save ZIP file path
NSString *savepath = [path stringbyappendingpathcomponent:[nsstring stringwithformat:@ "Book_%d.zip", [sender tag]];
// Initialize temporary file path
NSString *temppath = [path stringbyappendingpathcomponent:[nsstring stringwithformat:@ "Temp/book_%d.zip.temp", [ Sender tag]];
// Set File save path
[Request Setdownloaddestinationpath:savepath];
// Set temporary file path
[Request Settemporaryfiledownloadpath:temppath];
// set the agent for the progress bar ,
[Request Setdownloadprogressdelegate:cell];
// Set whether breakpoint download is supported
[Request Setallowresumeforfiledownloads:yes];
// set basic information
[Request Setuserinfo:[nsdictionary Dictionarywithobjectsandkeys:[nsnumber numberwithint:[sender tag]],@ "BookID", Nil]];
NSLog (@ "userinfo=%@", request.userinfo);
Add to asinetworkqueue queue to download
[Self.networkqueue Addoperation:request];
Recall Request
[Request release];
III. Suspension of requests
Here's the same as when downloading under the cell,
Customcell *cell = (Customcell *) [Self.mytableview cellforrowatindexpath:[nsindexpath indexPathForRow:[sender Tag] INSECTION:0]];
For (ASIHTTPRequest *request in [self.networkqueue operations]) {
Nsinteger BookID = [[Request.userinfo objectforkey:@ "BookID"] intvalue];// view userInfo information
if ([sender tag] = = BookID) {/ /Determine if ID matches
pausing a matching object
[Request Cleardelegatesandcancel];
}
}
Four, Asihttprequestdelegate callback method
The download request and the pause request are implemented, click Download, start downloading resources, when the point is paused, download interrupted, when we click the Download button, continue to download, in the second step of the
[Request Setallowresumeforfiledownloads:yes] Set whether breakpoint downloads are supported. Here are the following ways to implement the Asihttprequestdelegate proxy:
#pragma mark-
#pragma mark Asihttprequestdelegate method
Asihttprequestdelegate, download before the method of obtaining information , mainly to get the size of the download, can show the download progress how many bytes
-(void) Request: (ASIHTTPRequest *) Request Didreceiveresponseheaders: (Nsdictionary *) responseheaders {
NSLog (@ "didreceiveresponseheaders-%@", [responseheaders valueforkey:@ "Content-length"]);
NSLog (@ "contentlength=%f", request.contentlength/1024.0/1024.0);
int bookid = [[Request.userinfo objectforkey:@ "BookID"] intvalue];
Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
float Tempconlen = [[Userdefaults objectforkey:[nsstring stringwithformat:@ "Book_%d_contentlength", BookID]] Floatvalue];
NSLog (@ "tempconlen=%f", Tempconlen);
// if not saved , persists his content size
if (Tempconlen = = 0) {// if not saved , persist his content size
[Userdefaults setobject:[nsnumber numberwithfloat:request.contentlength/1024.0/1024.0] forKey:[NSString stringwithformat:@ "Book_%d_contentlength", BookID]];
}
}
Asihttprequestdelegate, the method to execute when the download is complete
-(void) requestfinished: (ASIHTTPRequest *) Request {
int bookid = [[Request.userinfo objectforkey:@ "BookID"] intvalue];
Customcell *cell = (Customcell *) [Self.mytableview Cellforrowatindexpath:[nsindexpath IndexPathForRow:bookid INSECTION:0]];
Cell.downloadcompletestatus = YES;
cell.progressView.progress = 0.0;
}
After the above steps, a support for the continuation of the function of the program is implemented!