1. Import the third-party class library ASIHTTPRequest
: Complete ASIHttpRequest class library code download: Download
2. In the. h file
#import <UIKit/UIKit.h>#import "ASIHTTPRequest.h"#import "ASINetworkQueue.h"@interface DownLoadViewController : UIViewController<ASIHTTPRequestDelegate>{ UIProgressView *_progressView;}@property(nonatomic, retain) UIProgressView *progressView;@property(nonatomic, retain) ASINetworkQueue *asiQueue;@property(nonatomic, retain) ASIHTTPRequest *asiHttpRequest;@end
3. implement this process in the. m file
Enable the queue first:
_ AsiQueue = [[ASINetworkQueue alloc] init]; // enable the queue [_ asiQueue reset]; // nil _ asiQueue. showAccurateProgress = YES; // progress [_ asiQueue go];
Download:
NSURL * url = [NSURL URLWithString: @ "request address"]; _ asiHttpRequest = [ASIHTTPRequest requestWithURL: url]; _ asiHttpRequest. delegate = self; _ asiHttpRequest. downloadProgressDelegate = self; // download progress proxy, used for resumable upload path = NSHomeDirectory (); // This method obtains the path of the application directory // target path, set a destination path to store the downloaded file NSString * savePath = [path stringByAppendingPathComponent: @ "qgw1_"];/* Temporary path: 1. set a temporary path to store files during the download process. after the download is complete, the file will be copied to the destination path, and the files in the temporary path will be deleted 3. Resumable upload: When the resumable upload attribute is set to YES, each execution will go to the temporary path to check whether the object to be downloaded exists and the download progress... Then the download will be continued on the basis of this, so as to achieve the effect of resumable upload set temporary path in this process is very important... */NSString * temp = [path stringByAppendingPathComponent: @ "temp"];/* adds an mp3 file to the temporary path, this is equivalent to setting a fake file to be downloaded, but it does not actually exist. It can be understood that a container is provided here, And the downloaded content is filled in this container. This container must be set. Otherwise, it will not know what to download... Some people may say: Why is it not the same as the above temporary path? NSString * temp = [path stringByAppendingPathComponent: @ "temp/qgwent"]; this is not acceptable, because your temporary path must be correct and owned, you need to use NSFileManager to determine whether such a path exists. If it does not exist, create it) so, which must be separated and written separately... */NSString * tempPath = [temp stringByAppendingPathComponent: @ "qgw1_"]; // create a file manager NSFileManager * fileManager = [NSFileManager defamanager]; // determine whether the temp folder contains BOOL fileExists = [fileManager fileExistsAtPath: temp]; if (! FileExists) {// If the folder does not exist, it is created because the [fileManager createDirectoryAtPath: temp withIntermediateDirectories: YES attributes: nil error: nil];} [_ asiHttpRequest setDownloadDestinationPath: savePath]; // download path [_ asiHttpRequest setTemporaryFileDownloadPath: tempPath]; // set a temporary path .. _ AsiHttpRequest. allowResumeForFileDownloads = YES; // enable the resumable upload. // progress bar _ progressView. alpha = 1.0f; _ progressView. progress = 0; // value 0 [_ asiQueue addOperation: _ asiHttpRequest]; // Add to the queue
Download progress:
// Proxy method to get the download progress-(void) setProgress :( float) newProgress {[_ progressView setProgress: newProgress]; // assign to progress bar}
Stop download:
[_ AsiHttpRequest clearDelegatesAndCancel]; // stop