iOS Development Web-file download using the ASI framework
Description : This article describes the framework ASI often used in iOS network programming, and how to use the framework for file downloads.
First, Brief introduction
code example:
#import"YYViewController.h"#import"ASIHTTPRequest.h"@interface Yyviewcontroller () @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload];}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //download the files on the server[SELF download];}#pragmamark-Download File-(void) download{//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip"]; ASIHTTPRequest*request=[ASIHTTPRequest Requestwithurl:url]; //2. Add request parameters (parameters in the request body)[Request setdatareceivedblock:^ (NSData *data) {NSLog (@"%d", data.length); }]; //3. Sending network requests asynchronously[Request startasynchronous];} @end
Code Description: The above code asynchronously downloads a file from the server and prints the length of the received data whenever the data is received.
The printing results are as follows:
Note : In the actual development can not download the file, because he constantly splicing file data operation is in memory, if the downloaded file data is large, it will directly lead to memory explosion.
Second, the actual development of the use of
code example (Demo 2):
#import"YYViewController.h"#import"ASIHTTPRequest.h"@interface Yyviewcontroller () @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload];}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //download the files on the server[self download1];}#pragmamark-Download File//Demo 1-(void) download{//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip"]; ASIHTTPRequest*request=[ASIHTTPRequest Requestwithurl:url]; //2. Add request parameters (parameters in the request body)[Request setdatareceivedblock:^ (NSData *data) {NSLog (@"%d", data.length); }]; //3. Sending network requests asynchronously[Request startasynchronous];}//Demo 2-(void) download1{//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip"]; ASIHTTPRequest*request=[ASIHTTPRequest Requestwithurl:url]; //2. Set the path to save the download fileNSString *cachepath=[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*filename=[cachepath stringbyappendingpathcomponent:@"Video.zip"]; Request.downloaddestinationpath=filename; NSLog (@"%@", filename); //3. Sending a network request (asynchronous)[Request startasynchronous]; //4. Notice when the download is complete[Request setcompletionblock:^{NSLog (@"Download Successful"); }];} @end
Download Successful:
Code Description:
In the actual development, if you want to use the ASI framework to download files on the server, just perform the following simple steps (refer to the code above).
(1) Create the Request object;
(2) Set the path to save the downloaded file;
(3) Network request (asynchronous) to send the download file.
Following the steps above, the program automatically turns on the asynchronous thread, writing data to the specified file path at 1.1 points, and no matter how large a file is downloaded, it does not consume a lot of memory space.
The ASI framework is based on the underlying cfnoteworking and has a good performance. Of course, you can also set blocks, or listen to the progress of the download.
The following describes how to download files using the ASI framework and how to listen for download progress.
Set the download agent, note that it is not a controller agent.
code example:
#import"YYViewController.h"#import"ASIHTTPRequest.h"@interface Yyviewcontroller ()<ASIProgressDelegate>@property (Weak, nonatomic) Iboutlet Uiprogressview*progress, @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload];}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //download the files on the server[SELF download];}#pragmamark-Download File-(void) download{//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip"]; ASIHTTPRequest*request=[ASIHTTPRequest Requestwithurl:url]; //2. Set the path to save the download fileNSString *cachepath=[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*filename=[cachepath stringbyappendingpathcomponent:@"Video.zip"]; Request.downloaddestinationpath=filename; NSLog (@"%@", filename); //3. Setting up the agent for download ProgressRequest.downloadprogressdelegate=self.progress; //4. Sending a network request (asynchronous)[Request startasynchronous]; //5. Notification after download is complete[Request setcompletionblock:^{NSLog (@"The file has been downloaded"); }];}#pragmamark-Agent method for monitoring download progress
ASI File Download There is also a property to set whether breakpoint downloads are supported.
Set the code to support the breakpoint download as follows:
Request.allowresumeforfiledownloads=yes;
In this case, for example, a file has been downloaded from 30 to the sandbox of the program, this time canceled the download. The next time you click to download the file, it will download the remaining 70 and 1.1-point writes to the sandbox.
Tip: The code to cancel the download is:
[Request Cleardelegatesandcancel];
Third, combining some of the progress shown by the three-party framework used
Go to Code4app. Download a third-party framework that shows the progress of the download, with Dacircularprogressview as an example.
After importing the necessary files for the framework, simply use the following.
code example:
#import"YYViewController.h"#import"ASIHTTPRequest.h"#import"DACircularProgressView.h"@interface Yyviewcontroller ()<ASIProgressDelegate>@property (Weak, nonatomic) Iboutlet Dacircularprogressview*Circularview, @property (weak, nonatomic) Iboutlet Uiprogressview*progress, @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload]; //set some of the basic propertiesSelf.circularview.tracktintcolor=[Uicolor Lightgraycolor]; Self.circularView.progressTintColor=[Uicolor Yellowcolor];}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //download the files on the server[SELF download];}#pragmamark-Download File-(void) download{//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip"]; ASIHTTPRequest*request=[ASIHTTPRequest Requestwithurl:url]; //2. Set the path to save the download fileNSString *cachepath=[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*filename=[cachepath stringbyappendingpathcomponent:@"Video.zip"]; Request.downloaddestinationpath=filename; NSLog (@"%@", filename); //3. Setting up the agent for download ProgressRequest.downloadprogressdelegate=Self.circularview; //4. Sending a network request (asynchronous)[Request startasynchronous]; //5. Set support Breakpoint Downloadrequest.allowresumeforfiledownloads=YES; //5. Notification after download is complete[Request setcompletionblock:^{NSLog (@"The file has been downloaded"); }];}#pragmamark-Agent method for monitoring download progress@end
Display effect:
Special tips:
iOS Development Web-file download using the ASI framework