1.導入ASIHTTPRequest第三方類庫app
下載地址:ASIHttpRequest類庫 完整代碼下載:下載oop
2.在 .h 文件中this
- #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.在 .m 文件中 實現這一過程atom
首先開啓隊列:url
- _asiQueue=[[ASINetworkQueue alloc]init];
- [_asiQueue reset];
- _asiQueue.showAccurateProgress=YES;
- [_asiQueue go];
實現下載:spa
- NSURL *url = [NSURL URLWithString:@"請求地址"];
-
- _asiHttpRequest=[ASIHTTPRequest requestWithURL:url];
- _asiHttpRequest.delegate=self;
- _asiHttpRequest.downloadProgressDelegate=self;
-
-
- path = NSHomeDirectory();
-
-
- NSString *savePath = [path stringByAppendingPathComponent:@"qgw.mp3"];
-
- NSString *temp = [path stringByAppendingPathComponent:@"temp"];
-
-
- NSString *tempPath = [temp stringByAppendingPathComponent:@"qgw.mp3"];
-
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
-
- BOOL fileExists = [fileManager fileExistsAtPath:temp];
- if (!fileExists)
- {
-
- [fileManager createDirectoryAtPath:temp
- withIntermediateDirectories:YES
- attributes:nil
- error:nil];
- }
-
- [ _asiHttpRequest setDownloadDestinationPath:savePath ];
- [ _asiHttpRequest setTemporaryFileDownloadPath:tempPath ];
-
- _asiHttpRequest.allowResumeForFileDownloads = YES;
-
-
- _progressView.alpha = 1.0f;
- _progressView.progress=0;
-
- [_asiQueue addOperation:_asiHttpRequest];
獲得下載的進度:.net
- - (void)setProgress:(float)newProgress{
- [_progressView setProgress:newProgress];
- }
中止下載:代理
- [_asiHttpRequest clearDelegatesAndCancel];