In the first two sections, I introduced the ASI "Synchronous and Asynchronous" and "File upload" knowledge, if you have any questions, please click here. This section provides a simple example of downloading a file through the ASI.
The interface is very simple, "start": Start downloading; "Stop": Pause download; Support for the continuation of the breakpoint.
1. to define a request member property, make a strong reference to the ASI request object.
@property (Nonatomic,strong) asihttprequest *request;
2. Implement proxy asiprogressdelegate to display progress bar progress.
3. in the Dealloc method, the request object of the ASI is destroyed.
When a request is made, perhaps 5 seconds after the response, did not wait for the response back when you switch the controller, then produced a wild pointer!!! So be sure to call the following code when the controller is destroyed!! -(void) dealloc { [self.request cleardelegatesandcancel];}
4. start and stop methods are as follows.
-(ibaction) Start { [self downloadFile];} -(Ibaction) Stop { [self.request cleardelegatesandcancel];}
5. The main way to implement the download function.
-(void) DownloadFile {//1. Create Request object (I use the test link, is the download Sogou Input method) Nsurl *url = [Nsurl urlwithstring:@ "Http://www.baidu.com/lin K?url= Bvctezsvwkmrvbm2cq5ntfsumx3ahzoevzpe6efbnemn8gfmkhc0bfxkoq1sfus9v-ww19u37ivbekdo5dwvryeaktk0upttbzz6xa5gf_y "]; Self.request = [ASIHTTPRequest Requestwithurl:url]; 2. File save path NSString *caches = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject]; After downloading the official file nsstring *filepath = [Caches stringbyappendingpathcomponent:@ "Sougou.zip"]; Self.request.downloadDestinationPath = FilePath; Temporary download file to support the continuation of the breakpoint. Successful continuation will automatically generate Sougou.zip nsstring *tempfilepath = [Caches stringbyappendingpathcomponent:@ "Sougou.temp.zip"]; Self.request.temporaryFileDownloadPath = TempFilePath; 3. Set the download progress Agent//with Self.progressview as the proxy, because Uiprogressview itself has setprogress method, equivalent to also implement the Setprogress proxy method. Can directly see the progressview of the progress of the show, double benefit. Self.request.downloadProgressDelegate = Self.progressview; Self.request.downLoadprogressdelegate = self; 4. Request Timeout Control self.request.timeOutSeconds = 10; 5. Support for the continuation of the breakpoint self.request.allowResumeForFileDownloads = YES; [Self.request startasynchronous];}
The code for the progress bar control in the code is interpreted: it actually takes advantage of the Setprogress method of the proxy with the same name as the progress property of the progress bar itself.
Self.request.downloadProgressDelegate = Self.progressview;
The code above is actually equivalent to
Self.request.downloadProgressDelegate = self;-(void) setprogress: (float) newprogress { //NSLog (@ "thread:%@", [ Nsthread CurrentThread]); self.progressView.progress = newprogress;}
So far, the introduction of the ASI section has been completed. In the following two sections, I will describe the use of AFN.
Introduction to ASI Usage (file download)