IOS自己封装的异步下载(get)Block方式回调给大家分享下
2014-11-29 15:04
344 查看
小牛一个希望大家多多关照。 上代码。
使用方法:造一个对象,调用
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
* @param updater block,下载成功调用的方法
*/
- (void)asynchronousDownload:(NSString *)url
success:(Success)success
failure:(Failure)failure;
方法。
BLOCK 回调, 成功或失败。
#import <Foundation/Foundation.h>
@class Downloader;
typedef void(^Success)(Downloader *downloader);
typedef void(^Failure)(NSError *error);
@interface Downloader : NSObject <NSURLConnectionDataDelegate>
{
long double _length;
NSMutableData *_datas;
}
@property (nonatomic, assign) long double length;
@property (nonatomic, strong) NSMutableData *datas;
@property (nonatomic, copy) Success success;
@property (nonatomic, copy) Failure failure;
@property (nonatomic, strong) NSString *strURL;
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
*/
- (void)asynchronousDownload:(NSString *)url;
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
* @param updater block,下载成功调用的方法
*/
- (void)asynchronousDownload:(NSString *)url
success:(Success)success
failure:(Failure)failure;
@end
#import "Downloader.h"
@implementation Downloader
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
*/
- (void)asynchronousDownload:(NSString *)strURL
{
NSLog(@"--->> Downloader 开始下载");
self.strURL = strURL;
NSURL *url = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:100];
[NSURLConnection connectionWithRequest:request delegate:self];
}
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
* @param success block,下载成功调用的方法
* @param failure block,下载失败调用的方法
*/
- (void)asynchronousDownload:(NSString *)url success:(Success)success failure:(Failure)failure
{
self.success = success;
self.failure = failure;
[self asynchronousDownload:url];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_length = response.expectedContentLength;
_datas = [NSMutableData dataWithCapacity:_length];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.datas appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"--->> Downloader 下载完成");
self.success(self);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"\n----------------download error----------------- \n%@", error);
self.failure(error);
}
使用方法:造一个对象,调用
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
* @param updater block,下载成功调用的方法
*/
- (void)asynchronousDownload:(NSString *)url
success:(Success)success
failure:(Failure)failure;
方法。
BLOCK 回调, 成功或失败。
#import <Foundation/Foundation.h>
@class Downloader;
typedef void(^Success)(Downloader *downloader);
typedef void(^Failure)(NSError *error);
@interface Downloader : NSObject <NSURLConnectionDataDelegate>
{
long double _length;
NSMutableData *_datas;
}
@property (nonatomic, assign) long double length;
@property (nonatomic, strong) NSMutableData *datas;
@property (nonatomic, copy) Success success;
@property (nonatomic, copy) Failure failure;
@property (nonatomic, strong) NSString *strURL;
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
*/
- (void)asynchronousDownload:(NSString *)url;
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
* @param updater block,下载成功调用的方法
*/
- (void)asynchronousDownload:(NSString *)url
success:(Success)success
failure:(Failure)failure;
@end
#import "Downloader.h"
@implementation Downloader
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
*/
- (void)asynchronousDownload:(NSString *)strURL
{
NSLog(@"--->> Downloader 开始下载");
self.strURL = strURL;
NSURL *url = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:100];
[NSURLConnection connectionWithRequest:request delegate:self];
}
/**
* @brief 异步下载网络资源
*
* @param strURL URL连接(字符串形式)
* @param success block,下载成功调用的方法
* @param failure block,下载失败调用的方法
*/
- (void)asynchronousDownload:(NSString *)url success:(Success)success failure:(Failure)failure
{
self.success = success;
self.failure = failure;
[self asynchronousDownload:url];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_length = response.expectedContentLength;
_datas = [NSMutableData dataWithCapacity:_length];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.datas appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"--->> Downloader 下载完成");
self.success(self);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"\n----------------download error----------------- \n%@", error);
self.failure(error);
}
相关文章推荐
- iOS将系统的UIAlertView代理方式改为Block回调封装
- iOS个人封装图片下载:Block与Delegate两种方式
- IOS网络篇10之NSURLConnection的使用(同步异步下载getpost)
- ios中使用block回调或代理方式请求网络接口数据(包括网络缓存)
- 自己封装的一个支持回调和独立线程的异步发送邮件的类
- Android OkHttp3(完美封装)Get异步获取数据、Post异步获取数据、Form表单提交、文件下载
- iOS简单封装网络请求,使用block回掉函数方式
- httpClient封装——异步、回调、下载
- iOS网络编程:三、GET代理方式进行异步请求
- 网络请求数据 get请求方式 post请求 协议异步连接服务器 block异步连接服务器
- 自己写的下载数据的方法(GET和POST两种)(已经封装)
- 四方力道源码(自己花钱大家分享) 下载了就知好用
- IOS中如何异步加载图片(一)封装自己的ImageDownLoad类
- iOS开发总结之block回调以AFN为基础封装的网络请求操作类
- 源码函数分享GetUrlByXmlHttp XMLHTTP方式下载网页
- iOS 同步,异步,普通 三种方式下载
- IOS:两种回调的方式实现(delegate和block)
- iOS 对于AFNetworking3.x版本的二次封装包含(GET、POST、上传、下载)
- IOS 数据库底层封装和Block回调