iOS growth path-asihttprequest breakpoint Continuation

Source: Internet
Author: User

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" >
    1. #import <UIKit/UIKit.h>
    2. #import "ASIHTTPRequest.h"
    3. #import "ASINetworkQueue.h"
    4. @interface downloadviewcontroller:uiviewcontroller<asihttprequestdelegate>
    5. {
    6. Uiprogressview *_progressview;
    7. }
    8. @property (nonatomic, retain) Uiprogressview *progressview;
    9. @property (nonatomic, retain) Asinetworkqueue *asiqueue;
    10. @property (nonatomic, retain) asihttprequest *asihttprequest;
    11. @end


3. Implement this process in a. m file

Start the queue first:

[CPP]View Plaincopy
    1. _asiqueue=[[asinetworkqueue Alloc]init]; //Open Queue
    2. [_asiqueue Reset]; //nil
    3. _asiqueue.showaccurateprogress=yes; //Progress
    4. [_asiqueue go];


To implement the download:

[CPP]View Plaincopy
  1. Nsurl *url = [Nsurl urlwithstring:@"request Address"];
  2. _asihttprequest=[asihttprequest Requestwithurl:url];
  3. _asihttprequest.delegate=self;
  4. _asihttprequest.downloadprogressdelegate=self; //Download Progress agent for breakpoint continuation
  5. Path = Nshomedirectory (); //This method gets the path to the application directory
  6. //destination path, set a destination path for storing downloaded files
  7. NSString *savepath = [path stringbyappendingpathcomponent:@"Qgw.mp3"];
  8. /* 
  9. Temporary path:
  10. 1. Set a temporary path to store files during the download process
  11. 2. The file will be copied to the destination path when it is loaded and the files in the temporary path are deleted.
  12. 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
  13. Setting a temporary path is quite important in this process ...
  14. */
  15. NSString *temp = [path stringbyappendingpathcomponent:@"temp"];
  16. /* 
  17. 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.
  18. This container must be set, otherwise it will not know what to download inside ...
  19. Someone will say: ask what does not and the above temporary path together, is not the same: nsstring *temp = [path stringbyappendingpathcomponent:@ "Temp/qgw.mp3"];
  20. 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,
  21. 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)
  22. So, to separate to write ...
  23. */
  24. NSString *temppath = [temp stringbyappendingpathcomponent:@"Qgw.mp3"];
  25. //Create File Manager
  26. Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
  27. //Determine if the Temp folder exists
  28. BOOL fileexists = [FileManager fileexistsatpath:temp];
  29. if (!fileexists)
  30. {//created if not present, because the folder is not automatically created when downloading
  31. [FileManager Createdirectoryatpath:temp
  32. Withintermediatedirectories:yes
  33. Attributes:nil
  34. Error:nil];
  35. }
  36. [_asihttprequest Setdownloaddestinationpath:savepath]; //download Path
  37. [_asihttprequest Settemporaryfiledownloadpath:temppath];  //temporary path, be sure to set the temporary path:
  38. _asihttprequest.allowresumeforfiledownloads = YES; //Open breakpoint, whether to resume the breakpoint
  39. //progress bar
  40. _progressview.alpha = 1.0f;
  41. _progressview.progress=0; //Assigned value is 0
  42. [_asiqueue Addoperation:_asihttprequest]; //Join queue


Get the download progress:

[CPP]View Plaincopy
    1. Agent method, get download progress
    2. -(void) setprogress: (float) newprogress{
    3. [_progressview setprogress:newprogress]; //Assign to progress bar
    4. }


To stop the download:

[CPP]View Plaincopy
    1. [_asihttprequest Cleardelegatesandcancel]; //Shut down

iOS growth path-asihttprequest breakpoint Continuation

Related Article

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.