iOS 自定义SZDownloader文件下载器封装及使用

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

允许暂停和恢复

允许下载与进度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值