asphttprequest 实现文件下载, 监听下载进度以及当前网络的状态(WXHL)
2013-08-25 10:28
791 查看
#import "AppDelegate.h"
#import "ASIHTTPRequest.h"
#import "Reachability.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(downloadAction) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(150, 30, 80, 30);
[button setTitle:@"下载" forState:UIControlStateNormal];
[self.window addSubview:button];
//kReachabilityChangedNotification 网络状态改变时触发的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNetwork:) name:kReachabilityChangedNotification object:nil];
self.reachability = [Reachability reachabilityForInternetConnection];
//开始监听网络
[self.reachability startNotifier];
NetworkStatus status = self.reachability.currentReachabilityStatus;
[self checkNetWork:status];
return YES;
}
- (void)downloadAction {
//---------------------ASI下载--------------------
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressView.frame = CGRectMake(50, 100, 200, 20);
[self.window addSubview:progressView];
//通过kvo监听progress值,达到监听进度的目的
[progressView addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
NSString *urlstring = @"http://free2.macx.cn:81/tools/system/CleanMyMac-v1-10-8.dmg";
NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fileName = [urlstring lastPathComponent];
NSString *path = [documents stringByAppendingPathComponent:fileName];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlstring]];
[request setHeadersReceivedBlock:^(NSDictionary *responseHeaders){
//响应头
// NSDictionary *responseHeaders = request.responseHeaders;
NSLog(@"%@",responseHeaders);
//获取下载文件大小
NSNumber *contentSize = [responseHeaders objectForKey:@"Content-Length"];
}];
//设置文件下载存放的路径
[request setDownloadDestinationPath:path];
//设置进度条
request.downloadProgressDelegate = progressView;
//------------------------断点续传-----------------------
//设置是否支持断点续传
[request setAllowResumeForFileDownloads:YES];
NSString *tempPath = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/cache.download"];
//设置下载过程中暂存的文件路径
[request setTemporaryFileDownloadPath:tempPath];
[request startAsynchronous];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
// NSLog(@"%@",change);
NSNumber *value = [change objectForKey:@"new"];
float progress = [value floatValue];
NSLog(@"%.1f%%",progress*100);
}
//网络状态改变的时候调用
- (void)changeNetwork:(NSNotification *)notification {
NetworkStatus status = self.reachability.currentReachabilityStatus;
[self checkNetWork:status];
}
- (void)checkNetWork:(NetworkStatus)status {
if (status == kNotReachable) {
NSLog(@"没有网络");
}
else if(status == kReachableViaWWAN) {
NSLog(@"3G/2G");
}
else if(status == kReachableViaWiFi) {
NSLog(@"WIFI");
}
}
@end
相关文章推荐
- 利用ASIHttp实现文件的下载、上传和网络状态的监控
- HttpClient 4.5版本实现文件上传下载以及常用post、get请求
- ASP.NET 2.0 HttpHandler实现对某种文件类型权限保护(示例代码下载)
- 使用HttpWebRequest以及HttpWebResponse下载Http远程文件[转]
- XMLHttpRequest上传文件实现进度条
- ASP.NET利用HttpHandler实现多扩展名文件下载
- 利用XMLHttpRequest技术实现断点续传下载文件
- [Javascript]XMLHttpRequest对象实现下载进度条
- Android中使用AsyncTask实现文件下载以及进度更新提示
- [导入]ASP.NET 2.0 HttpHandler实现对某种文件类型权限保护(示例代码下载)
- Android网络编程之使用HttpClient批量上传文件(二)AsyncTask+HttpClient并实现上传进度监听
- android网络编程之HttpUrlConnection的讲解--实现文件断点下载
- ASP.NET利用HttpHandler实现多扩展名文件下载 [转]
- ASP.NET利用HttpHandler实现多扩展名文件下载
- ASP.NET 2.0 HttpHandler实现对某种文件类型权限保护(示例代码下载)
- IOS网络通信之ASIHttpRequest下载/上传进度的追踪
- ASP.NET利用HttpHandler实现多扩展名文件下载
- WebRequest之HttpWebRequest实现服务器上文件的下载(一)
- Android中使用AsyncTask实现文件下载以及进度更新提示
- ASIHttpRequest框架使用说明-----post请求 以及监听网络