iOS Development Web-file download using the ASI framework

Source: Internet
Author: User

Original http://www.cnblogs.com/wendingding/p/3961681.html

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:

 1#import"YYViewController.h"2#import"ASIHTTPRequest.h"34 @Interface Yyviewcontroller ()567 @end89 @implementation Yyviewcontroller1011-(void) Viewdidload12 {[Super Viewdidload];14}1516-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event17 {18//Download the files on the server[SELF DOWNLOAD];20}21st22#pragmamark-Download File23-(void) Download24 {//1. Create Request Object 25 Nsurl *url=[nsurl urlwithstring:< Span class= "string" >@ "http://127.0.0.1:8080/mjserver/resources/video.zip"]; 26 asihttprequest *request=[asihttprequest Requestwithurl:url]; 27 28 //2. Add request parameters (parameters in the request body) Span class= "number" >29 [Request setdatareceivedblock:^ (NSData *data) {30 NSLog ( @ "%d", data.length); 31}]; 32 33 //3. Sending network requests asynchronously 34 [request startasynchronous]; 35} 36 37 @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):

 1#import"YYViewController.h"2#import"ASIHTTPRequest.h"34 @Interface Yyviewcontroller ()567 @end89 @implementation Yyviewcontroller1011-(void) Viewdidload12 {[Super Viewdidload];14}1516-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event17 {18//Download the files on the server[Self download1];20}21st22#pragmamark-Download File23//Demo 124-(void) Download25 {//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip "];ASIHTTPRequest *request=[asihttprequest Requestwithurl:url];2829//2. Add request parameters (parameters in the request body)[Request setdatareceivedblock:^ (NSData *data) {NSLog (@"%d ", data.length);32}];3334//3. Sending network requests asynchronously[Request startasynchronous];36}3738//Demo 239-(void) Download140 {41//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip "];ASIHTTPRequest *request=[asihttprequest Requestwithurl:url];4445//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. Send network request (asynchronous)[request startasynchronous];   4. Notify when the download is complete [request setcompletionblock:^{NSLog (@ " downloaded successfully");  57}]; 58}  @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:

 1#import"YYViewController.h"2#import"ASIHTTPRequest.h"34 @Interface Yyviewcontroller () <ASIProgressDelegate>5 @property (weak, nonatomic) Iboutlet Uiprogressview *progress;6 @end78 @implementation Yyviewcontroller910-(void) Viewdidload11 {[Super Viewdidload];13}1415-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event16 {17//Download the files on the server[SELF DOWNLOAD];19}2021st#pragmamark-Download File22-(void) Download23 {24//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip "];ASIHTTPRequest *request=[asihttprequest Requestwithurl:url];2728//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); 33 34 //3. Set the agent for download progress 35 request.downloadprogressdelegate=self.progress; 36 37 //4. Send network request (async) 38 [request startasynchronous]; 39 40 //5. Notify 41 [request Setcompletionblock:^{42 NSLog (@ " file has been downloaded"); 43}]; 44} 45 46 #pragma mark-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:

 1#import"YYViewController.h"2#import"ASIHTTPRequest.h"3#import"DACircularProgressView.h"45 @Interface Yyviewcontroller () <ASIProgressDelegate>67 @property (weak, nonatomic) Iboutlet Dacircularprogressview *circularview;8 @property (weak, nonatomic) Iboutlet Uiprogressview *progress;9 @end10@implementation Yyviewcontroller1213-(void) Viewdidload14 {[Super Viewdidload];1617//Set some of the basic propertiesSelf.circularview.tracktintcolor=[uicolor Lightgraycolor];Self.circularview.progresstintcolor=[uicolor Yellowcolor];20}21st22-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event23 {24//Download the files on the server[SELF DOWNLOAD];26}2728#pragmamark-Download File29-(void) Download30 {31//1. Create a Request objectNsurl *url=[nsurl urlwithstring:@"Http://127.0.0.1:8080/MJServer/resources/video.zip "];ASIHTTPRequest *request=[asihttprequest Requestwithurl:url];3435//2. Set the path to save the download fileNSString *cachepath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] ;Panax Notoginseng nsstring *filename=[cachepath stringbyappendingpathcomponent:@"Video.zip "];Request.downloaddestinationpath=filename;NSLog (@"%@ ", filename);4041//3. Setting up the agent for download progressRequest.downloadprogressdelegate=self.circularview;43 44 //4. Send network request (async) 45 [request startasynchronous]; 46 47 //5. Set support breakpoints Download 48 request.allowresumeforfiledownloads=yes; 49 50 //5. Notify 51 [request Setcompletionblock:^{52 NSLog (@ " file has been downloaded"); 53}]; 54} 55 56 #pragma mark-agent method for monitoring download Progress 57 @end        

Display effect:

Special tips:

iOS Development Web-file download using the ASI framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.