1. Import asihttprequest third-party class libraries
: ASIHTTPRequest class Library full code download: download
2. In the. h file
[CPP]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- #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 a. m file
Start the queue first:
[CPP]View Plaincopy
- _asiqueue=[[asinetworkqueue Alloc]init]; //Open Queue
- [_asiqueue Reset]; //nil
- _asiqueue.showaccurateprogress=yes; //Progress
- [_asiqueue go];
To implement the download:
[CPP]View Plaincopy
- Nsurl *url = [Nsurl urlwithstring:@"request Address"];
- _asihttprequest=[asihttprequest Requestwithurl:url];
- _asihttprequest.delegate=self;
- _asihttprequest.downloadprogressdelegate=self; //Download Progress agent for breakpoint continuation
- Path = Nshomedirectory (); //This method gets the path to the application directory
- //destination path, set a destination path for storing downloaded files
- NSString *savepath = [path stringbyappendingpathcomponent:@"Qgw.mp3"];
- /*
- Temporary path:
- 1. Set a temporary path to store files during the download process
- 2. The file will be copied to the destination path when it is loaded and the files in the temporary path are deleted.
- 3. Continuation of the breakpoint: when the setting of the continuation of the property is yes, each execution will go to the temporary path to find the file to be downloaded, download the progress and so on ... Then it will continue to download, so as to achieve the effect of continued transmission
- Setting a temporary path is quite important in this process ...
- */
- NSString *temp = [path stringbyappendingpathcomponent:@"temp"];
- /*
- Also in the temporary path to add a MP3 format file, which is equivalent to set up a fake to download the file, actually does not exist, it can be understood that: here is a container, the content of the download is populated in this container.
- This container must be set, otherwise it will not know what to download inside ...
- Someone will say: ask what does not and the above temporary path together, is not the same: nsstring *temp = [path stringbyappendingpathcomponent:@ "Temp/qgw.mp3"];
- This is not possible, because your temporary path must be guaranteed to be correct, is owned, so in the following you have to use Nsfilemanager to determine if there is a path, if not exist to create,
- When you create the time will be Qgw.mp3 as a folder to create, so every time the breakpoint continued to go into the Qgw.mp3 folder to find, of course, is not found (because Qwg.mp3 is)
- So, to separate to write ...
- */
- NSString *temppath = [temp stringbyappendingpathcomponent:@"Qgw.mp3"];
- //Create File Manager
- Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
- //Determine if the Temp folder exists
- BOOL fileexists = [FileManager fileexistsatpath:temp];
- if (!fileexists)
- {//created if not present, because the folder is not automatically created when downloading
- [FileManager Createdirectoryatpath:temp
- Withintermediatedirectories:yes
- Attributes:nil
- Error:nil];
- }
- [_asihttprequest Setdownloaddestinationpath:savepath]; //download Path
- [_asihttprequest Settemporaryfiledownloadpath:temppath]; //temporary path, be sure to set the temporary path:
- _asihttprequest.allowresumeforfiledownloads = YES; //Open breakpoint, whether to resume the breakpoint
- //progress bar
- _progressview.alpha = 1.0f;
- _progressview.progress=0; //Assigned value is 0
- [_asiqueue Addoperation:_asihttprequest]; //Join queue
Get the download progress:
[CPP]View Plaincopy
- Agent method, get download progress
- -(void) setprogress: (float) newprogress{
- [_progressview setprogress:newprogress]; //Assign to progress bar
- }
To stop the download:
[CPP]View Plaincopy
- [_asihttprequest Cleardelegatesandcancel]; //Shut down
iOS growth path-asihttprequest breakpoint Continuation