OC第三方框架-ASI使用方法

from:http://blog.csdn.net/iphone13/article/details/9674819


  1. #import "ViewController.h"  

  2. #import "ASIHTTPRequest.h"  

  3. #import "SSZipArchive.h"  

  4. @interface ViewController ()  

  5. {  

  6.    CGFloat _fileLength;  

  7. }  

  8. @end  

  9. @implementation ViewController  

  10. - (void)viewDidLoad  

  11. {  

  12.    [superviewDidLoad];  

  13. // Do any additional setup after loading the view, typically from a nib.  

  14. }  

  15. - (void)didReceiveMemoryWarning  

  16. {  

  17.    [superdidReceiveMemoryWarning];  

  18.    // Dispose of any resources that can be recreated.  

  19. }  

  20. #pragma mark - ASI框架断点续传下载文件  

  21. - (IBAction)downloadFile {  

  22.    //指定下载地址  

  23.    NSString *urlstring = [NSStringstringWithFormat:@"http://teacher.local/~apple/xxx/download/iTunesConnect_DeveloperGuide_CN.zip"];  

  24.    NSURL *url = [NSURLURLWithString:urlstring];  

  25.    //设定文件保存路径及临时缓存路径  

  26.    NSArray *documents =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  

  27.    NSString *downloadPath = [documents[0]stringByAppendingPathComponent:@"book.zip"];  

  28.    NSString *tempPath = [documents[0]stringByAppendingPathComponent:@"book.tmp"];  

  29.    //创建ASIHTTPRequest  

  30.    ASIHTTPRequest *request = [[[ASIHTTPRequestalloc]initWithURL:url]autorelease];  

  31.    //设置代理,ASI是通过代理回调的方式处理网络请求的  

  32.    [request setDelegate:self];  

  33.    //设置下载路径  

  34.    [request setDownloadDestinationPath:downloadPath];  

  35.    //设置缓存路径  

  36.    [request setTemporaryFileDownloadPath:tempPath];  

  37.    //设置允许断点续传  

  38.    [request setAllowResumeForFileDownloads:YES];  

  39.    //设置下载进程代理,实现显示下载进度情况  

  40.    [request setDownloadProgressDelegate:self];  

  41.    //启动异步请求  

  42.    [request start];  

  43. }  

  44. //去.h文件中遵守协议  

  45. #pragma mark - ASI的代理方法  

  46. //请求开始  

  47. - (void)requestStarted:(ASIHTTPRequest *)request  

  48. {  

  49.    NSLog(@"请求开始");  

  50. }  

  51. //接收到响应头  

  52. - (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders  

  53. {  

  54.    //通过NSLog看到文件大小的key是“Content-length”  

  55.    NSLog(@"请求头%@", responseHeaders);  

  56. }  

  57. //请求完成  

  58. - (void)requestFinished:(ASIHTTPRequest *)request  

  59. {  

  60.    NSLog(@"请求完成");  

  61.    //在这个方法里做解压缩工作  

  62.    //定义一个压缩文件  

  63.    NSArray *documents =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  

  64.    NSString *downloadPath = [documents[0]stringByAppendingPathComponent:@"book.zip"];  

  65.    //定义一个目标解压目录  

  66.    //这里是documents[0]这个目录  

  67.    //调用类方法解压缩  

  68.    [SSZipArchive unzipFileAtPath:downloadPathtoDestination:documents[0]];  

  69.    //删除压缩文件  

  70.    [[NSFileManagerdefaultManager]removeItemAtPath:downloadPatherror:nil];  

  71. }  

  72. //请求失败  

  73. - (void)requestFailed:(ASIHTTPRequest *)request  

  74. {  

  75.    NSLog(@"请求失败");  

  76. }  

  77. #pragma mark - 下载进程代理方法  

  78. - (void)setProgress:(float)newProgress  

  79. {  

  80.    NSLog(@"%.2f", newProgress *_fileLength);  

  81. }  

  82. @end


你可能感兴趣的:(import,interface,resources,第三方)