Github源码:https://github.com/manguoge/SZDownloader
SZDownloader是一个用于下载文件的小型库,简洁并且容易使用。您可以使用Block或Delegate下载文件。如果您正在使用TableView,您应该使用Delegate而不是Block。
##如何使用
下载SZDownloader.h和SZDownloader.m并添加到你的项目。
###使用块
SZDownloader * downloader = [[SZDownloader alloc] initWithURL:[NSURL URLWithString:@“http://myfile.com/file.jpg”] timeout:60];
[downloader startWithDownloading:(float progress,NSInteger percentage){
//进度条进度
//下载百分比的百分比
} onFinished :( NSData * fileData,NSString * fileName){
//使用NSData编写文件或图像
} onFail(NSError * error){
//失败
}];
###使用Delegate
遵循SZDownloaderDelegate协议
SZDownloader * downloader = [[SZDownloader alloc] initWithURL:[NSURL URLWithString:@“http://myfile.com/file.jpg”] timeout:60];
[downloader startWithDelegate:self];//委托方法是必需的
- (void)SGDownloadProgress:(float)progress百分比:(NSInteger)percent;
- (void)SGDownloadFinished :( NSData *)fileData;
- (void)SGDownloadFail:(NSError *)error;
允许1-5个并发下载1个文件(如下载管理器)
#使用块的示例
-(void)download
{
_requestStringURL=kCloudURL;
NSURL *requestURL=[NSURL URLWithString:_requestStringURL];
SZDownloader *downloader=[[SZDownloader alloc] initWithURL:requestURL timeout:6.0];
[downloader startWithDownloading:^(float progressValue, NSInteger percentage)
{
NSLog(@"progressValue=%f,percentage=%ld",progressValue,percentage);
} onFinished:^(NSData *fileData)
{
NSDictionary *jsonDict=[NSJSONSerialization JSONObjectWithData:fileData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"jsonDict=%@",jsonDict);
[downloader cancel];
}
onFail:^(NSError *error)
{
NSLog(@"auth error=%@",error);
[downloader cancel];
}];
}
##更新日志
###版本0.2
允许暂停和恢复
允许下载与进度