主要是用到了一个自定义的下载类,例子中包括了:同步下载、异步下载、身份验证。
DownLoadHelper.h
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @protocol DownLoadHelperDelegate <NSObject> @optional -(void) didReceiveData:(NSData *)theData; -(void) didReceiveFilename:(NSString *)aName; -(void) dataDownloadFailed:(NSString *)reason; -(void) dataDownloadAtPercent:(NSNumber *)aPercent; @end @interface DownLoadHelper : NSObject { NSURLResponse *respone; NSMutableData *data; NSString *urlString; NSURLConnection *urlconnection; id <DownLoadHelperDelegate> delegate; BOOL isDownloading; NSString *username; NSString *userpassword; } @property(nonatomic ,retain) NSURLResponse *respone; @property(nonatomic ,retain) NSMutableData *data; @property(nonatomic ,retain) NSString *urlString; @property(nonatomic ,retain) NSURLConnection *urlconnection; @property(nonatomic ,retain) id delegate; @property(assign) BOOL isDownloading; @property(nonatomic ,retain) NSString *username; @property(nonatomic ,retain) NSString *userpassword; +(DownLoadHelper *) sharedInstance; +(void) download:(NSString *)aURLString; +(void) cancel; @end
DownLoadHelper.m
#import "DownLoadHelper.h" static DownLoadHelper *sharedInstance = nil; @implementation DownLoadHelper @synthesize respone; @synthesize data; @synthesize urlString; @synthesize urlconnection; @synthesize delegate; @synthesize isDownloading; @synthesize username,userpassword; -(void)start{ self.isDownloading = NO; NSURL *url = [NSURL URLWithString:self.urlString]; if (!url) { NSString *reason = [NSString stringWithFormat:@"Could not create URL from string %@",self.urlString]; if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(dataDownloadFailed:)]) { [sharedInstance.delegate performSelector:@selector(dataDownloadFailed:) withObject:reason]; return; } } NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; if (!theRequest) { NSString *reason = [NSString stringWithFormat:@"Could not create URL request from string %@",self.urlString]; if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(dataDownloadFailed:)]) { [sharedInstance.delegate performSelector:@selector(dataDownloadFailed:) withObject:reason]; return; } } self.urlconnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (!self.urlString) { NSString *reason = [NSString stringWithFormat:@"URL connection failed for string %@",self.urlString]; if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(dataDownloadFailed:)]) { [sharedInstance.delegate performSelector:@selector(dataDownloadFailed:) withObject:reason]; return; } } self.isDownloading = YES; self.data = [NSMutableData data]; self.respone = nil; [self.urlconnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; } -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ if (!username || !userpassword) { [[challenge sender] useCredential:nil forAuthenticationChallenge:challenge]; return; } NSURLCredential *cred = [[[NSURLCredential alloc] initWithUser:self.username password:self.userpassword persistence:NSURLCredentialPersistenceNone] autorelease]; [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge]; } -(void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ NSLog(@"Challenge cancelled"); } -(void)cleanup{ self.data = nil; self.respone = nil; self.urlconnection = nil; self.urlString = nil; self.isDownloading = NO; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aresponse{ self.respone = aresponse; if ([aresponse expectedContentLength] < 0) { NSString *reason = [NSString stringWithFormat:@"Invalid URL [%@]",self.urlString]; if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(dataDownloadFailed:)]) { [sharedInstance.delegate performSelector:@selector(dataDownloadFailed:) withObject:reason]; } [connection cancel]; [self cleanup]; return; } if ([aresponse suggestedFilename]) { if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(didReceiveFilename:)]) { [sharedInstance.delegate performSelector:@selector(didReceiveFilename:) withObject:[aresponse suggestedFilename]]; } } } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{ [self.data appendData:theData]; if (self.respone) { float exectedlength = [self.respone expectedContentLength]; float currentLength = self.data.length; float percent = currentLength / exectedlength; if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(dataDownloadAtPercent:)]) { [sharedInstance.delegate performSelector:@selector(dataDownloadAtPercent:) withObject:[NSNumber numberWithFloat:percent]]; } } } -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ self.respone = nil; if (self.delegate) { NSData *theData = [self.data retain]; if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(didReceiveData:)]) { [sharedInstance.delegate performSelector:@selector(didReceiveData:) withObject:theData]; } } [self.urlconnection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; [self cleanup]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ self.isDownloading = NO; NSLog(@"Error:failed connection ,%@",[error localizedDescription]); if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(dataDownloadFailed:)]) { [sharedInstance.delegate performSelector:@selector(dataDownloadFailed:) withObject:@"Failed Connection"]; } [self cleanup]; } +(DownLoadHelper *)sharedInstance{ if (!sharedInstance) { sharedInstance = [[self alloc] init]; } return sharedInstance; } +(void)download:(NSString *)aURLString{ if (sharedInstance.isDownloading) { NSLog(@"Error:Cannot start new download until current download finishes"); if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(didReceiveData:)]) { [sharedInstance.delegate performSelector:@selector(didReceiveData:) withObject:@""]; } return; } sharedInstance.urlString = aURLString; [sharedInstance start]; } +(void)cancel{ if (sharedInstance.isDownloading) { [sharedInstance.urlconnection cancel]; } } @end
相关推荐
豆丁网免费下载器(冰点)无需登录也无需积分,只需要将需要下载的文档的网页地址复制在软件的录入框中就可以自由下载,并最终生成pdf文件。对部分pdf文件能够提取文字生成txt。 豆丁网免费下载器还支持自由下载百度、...
用ajax实现的java文件打包下载。可以输入扩展名选择文件下载。
在线下载的方法(付费一2般不可能全部下载!
由于在学习numpy数据分析的时候没有csv类型文件,又懒得自己一个个敲字,便想来csdn里随便下载一个。然而......居然要钱啊!!!!!!!我没钱,所以自己写了个,我不要你们的钱,麻烦各位拿资源的时候吱一声,这是...
visual studio 2010的帮助文档很难下载,一方面文件很多很大,全部文档超过1GB,使用微软自带的 或者辅助工具,由于没有发挥多线程和断点续传的特点,下载速度很慢,容易失败。 另一方面迅雷等下载工具,可以实现...
ASP.NET中提供可以在页面下载存储在后台的文件,很好用哦
用Visuall C++实现远程文件下载,这只是word文档。。。
将豆丁文档转化为pdf 免除豆丁收费要求
utf-8文档下载,有需要的可以下载一下,
豆丁网文件一般只能看,不能下,本工具可把豆丁网文件下载到本地,并保存成pdf文件
库函数stdio.h文件下载~………………
1、在本站下载解压,得到小叶文库下载器软件包; 2、首先前往文库网站找到需要下载的文库 3、将其文库地址复制粘贴到软件中,进行解析下载;...4、之后下载好的文件可在本自录下的Download文件夹进行查看。
redis.conf Redis配置文件 下载 redis.conf 配置详解 Redis配置文件redis.conf 详解1.基本配置内存单位的表示# 1k => 1000 bytes# ...Redis 的详细介绍Redis 的下载地址
Java实现文件下载功能,以WORD文档方式展现给用户看。
豆丁网文档下载器 第一次运行请先运行bat文件,当文件处理时请保证您的当前磁盘有足够的空间
python
Visual Studio 2012 官方离线文档 下载地址
sharepoint 2010 实现文档下载次数
文件下载的jsf程序...................(用poi导出excel文档)
[download id="1,2,3" category="4,5,6"] 为显示多个下载文件为1,2,3的文档,category=”4,5,6″为对应的分类ID。 b.在页面中添加下载:当你添加页面时在内容框插入:[page_download]或[page_download category=...