iOS Development Web-file download using the ASI framework

Source: Internet
Author: User

First, Brief introduction

code example:

1 #import "YYViewController.h" 2 #import "ASIHTTPRequest.h" 3  4 @interface Yyviewcontroller () 5  6  7 @end 8
   9 @implementation YYViewController10-(void) viewDidLoad12 {     [Super viewdidload];14}15-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event17 {     //download file on server     [self download];20}21 #pragma mark-download file (void) download24 {    //1. Create Request Object     Nsurl *url=[nsurl urlwithstring:@ "http://127.0.0.1:8080/ Mjserver/resources/video.zip "];26     asihttprequest *request=[asihttprequest requestwithurl:url];27     28     //2. Adding request parameters (parameters in the request body)     [Request setdatareceivedblock:^ (NSData *data)         { NSLog (@ "%d", data.length),     }];32     //3. Asynchronous send network request     [request startasynchronous];35}36 Notoginseng @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" 3 4 @interface Yyviewcontroller () 5 6 7 @end 8 9 @implem Entation YYViewController10-(void) viewDidLoad12 {[Super viewdidload];14}15-(void) Touchesbegan: (Nsset *) to Uches withevent: (uievent *) event17 {18//download file on server [self download1];20}21 #pragma mark-download file 23//Demo 124-(void ) download25 {//1. Create request object Nsurl *url=[nsurl urlwithstring:@ "Http://127.0.0.1:8080/MJServer/resources/video.zip"];2 7 asihttprequest *request=[asihttprequest requestwithurl:url];28 29//2. Add request parameters (parameters in the request body). [Request SetData receivedblock:^ (NSData *data) {NSLog (@ "%d", data.length); 32}];33 34//3. Sending network requests asynchronously [request STA RTASYNCHRONOUS];36}37 38//Demo 239-(void) download140 {41//1. Create Request object. Nsurl *url=[nsurl urlwithstring:@ "http://127     .0.0.1:8080/mjserver/resources/video.zip "];43 asihttprequest *request=[asihttprequest requestWithURL:url];44 45     2. Set the path to save the download file 46NSString *cachepath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject]; NSString *filename=[cachepath stringbyappendingpathcomponent:@ "Video.zip"];48 request.downloaddestinationpath=f ilename;49 NSLog (@ "%@", filename); 50 51//3. Send Network request (async) [Request startasynchronous];53 54//4. Now that you're done Tighten notice [request setcompletionblock:^{56 NSLog (@ "Download Success");}];58}59 @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" 3 4 @interface Yyviewcontroller () <asiprogressdelegate&gt ; 5 @property (weak, nonatomic) Iboutlet Uiprogressview *progress; 6 @end 7 8 @implementation Yyviewcontroller 9-(void) ViewDidLoad11 {[Super viewdidload];13}14-(void) touch Esbegan: (Nsset *) touches withevent: (uievent *) event16 {17//download files on the server [self download];19}20 #pragma mark-upload Piece (void) download23 {24//1. Create Request object Nsurl *url=[nsurl urlwithstring:@ "http://127.0.0.1:8080/MJServer/resources/ Video.zip "];26 asihttprequest *request=[asihttprequest requestwithurl:url];27 28//2. Set the path to save the download file NSStrin     G *cachepath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];30 NSString *filename=[cachepath stringbyappendingpathcomponent:@ "Video.zip"];31 request.downloaddestinationpath= Filename;32 NSLog (@ "%@", filename); 33 34//3. Set up agent for download progress REQUEST.DOWNLOADPROGRESSDELEGATE=SELF.PROGRESS;36 37//4. Send Network request (async) [Request startasynchronous];39 40//5. Notify when download is complete [requ EST setcompletionblock:^{42 NSLog (@ "file has been downloaded"),}];44}45 #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" 4 5 @interface Yyviewco Ntroller () <ASIProgressDelegate> 6 7 @property (weak, nonatomic) Iboutlet Dacircularprogressview *circularview; 8 @property (weak, nonatomic) Iboutlet Uiprogressview *progress; 9 @end10 @implementation YYViewController12-(void) VIEWDIDLOAD14 {[Super viewdidload];16 17//Set basic one Some properties self.circularview.tracktintcolor=[uicolor lightgraycolor];19 self.circularview.progresstintcolor=[uicolor ye Llowcolor];20}21-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event23 {24//download files on server [self Dow nload];26}27 #pragma mark-download file-(void) Download30 {31//1. Create Request object Nsurl *url=[nsurl urlwithstring:@ "HTTP://1     27.0.0.1:8080/mjserver/resources/video.zip "];33 asihttprequest *request=[asihttprequest requestWithURL:url];34 35 2. Set the path of the downloaded file save NSString *cachepath=[nssearchpathfordirectoriesindomains (NSdocumentdirectory, Nsuserdomainmask, YES) lastobject];37 nsstring *filename=[cachepath stringbyappendingpathcomponent:@ "Video.zip"];38 request.downloaddestinationpath=filename;39 NSLog (@ "%@", filename     ); 40 41//3. Set the agent for download progress request.downloadprogressdelegate=self.circularview;43 44//4. Send Network request (async) 45 [Request startasynchronous];46 47//5. Set support breakpoints Download request.allowresumeforfiledownloads=yes;49 50//5. Download complete  After completion of the notice to the [Request setcompletionblock:^{52 NSLog (@ "file has been downloaded");}];54}55 #pragma mark-agent method for monitoring download Progress @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.