一,AFNetworking跟SDWebImge是功能强大且常用的第三方,然而在实际应用中需要封装用来复用今天就跟大家分享一下AFN&SDWeb的二次封装
1.
HttpClient.h及.m
.h定义了HTTP常用的四种GET POST PUT DELETE 请求
一般分别用做增删减查
定义前预处理,请求成功处理,请求失败处理的block
及带多参数w的方法声明
.m设置请求类型跟响应类型,监听网络是否有网络,如果没有,弹出网络异常的警示框
2.
UIImageView+WebCache.h及.m
这是类别的方法,把网络请求图片封装起来
.h 定义加载成功,加载失败及加载进度的block
下载未成功时预设图片的加载方法及网络请求下载图片的方法声明
.m
是两个方法的具体实现
1 #import <Foundation/Foundation.h> 2 #import "AFNetworking.h" 3 4 //HTTP请求类别 5 typedef NS_ENUM(NSInteger,HttpRequestType) { 6 HttpRequestGet, 7 HttpRequestPost, 8 HttpRequestPut, 9 HttpRequestDelete, 10 }; 11 12 13 /** 14 * 请求前预处理block 15 */ 16 typedef void(^PrepareExecuteBlock)(void); 17 18 typedef void(^SuccessBlock)(NSURLSessionDataTask * task, id responseObject); 19 20 typedef void(^FailureBlock)(NSURLSessionDataTask * task, NSError * error); 21 22 @interface HttpClient : NSObject 23 24 + (HttpClient *)defaultClient; 25 26 /** 27 * HTTP请求(GET,POST,PUT,DELETE) 28 * 29 * @param url 请求地址 30 * @param method 请求类型 31 * @param params 请求参数 32 * @param prepare 请求前预处理 33 * @param success 请求成功处理 34 * @param failure 请求失败处理 35 */ 36 37 - (void)requestWithPath:(NSString *)url 38 method:(NSInteger)method 39 paramenters:(NSDictionary *)params 40 prepareExecute:(PrepareExecuteBlock)prepare 41 success:(SuccessBlock)success 42 failure:(FailureBlock)failure; 43 44 45 @end
1 #import "HttpClient.h" 2 3 @interface HttpClient () 4 5 @property (nonatomic, strong) AFHTTPSessionManager * manager; 6 7 @property (nonatomic, assign) BOOL isConnect; 8 9 @end 10 11 @implementation HttpClient 12 13 - (instancetype)init 14 { 15 self = [super init]; 16 if (self) { 17 18 self.manager = [AFHTTPSessionManager manager]; 19 //设置请求类型 20 self.manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 21 //设置响应类型 22 self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; 23 24 self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"text/plain",@"image/gif", nil]; 25 26 //开启监听 27 [self openNetMonitoring]; 28 29 } 30 return self; 31 } 32 33 - (void)openNetMonitoring { 34 35 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 36 37 switch (status) { 38 case AFNetworkReachabilityStatusUnknown: 39 self.isConnect = NO; 40 break; 41 case AFNetworkReachabilityStatusNotReachable: 42 self.isConnect = NO; 43 break; 44 case AFNetworkReachabilityStatusReachableViaWiFi: 45 self.isConnect = YES; 46 break; 47 case AFNetworkReachabilityStatusReachableViaWWAN: 48 self.isConnect = YES; 49 break; 50 default: 51 break; 52 } 53 54 }]; 55 56 [[AFNetworkReachabilityManager sharedManager] startMonitoring]; 57 58 self.isConnect = YES; 59 } 60 61 + (HttpClient *)defaultClient { 62 63 static HttpClient * instance = nil; 64 static dispatch_once_t onceToken; 65 dispatch_once(&onceToken, ^{ 66 instance = [[self alloc] init]; 67 }); 68 return instance; 69 } 70 71 - (void)requestWithPath:(NSString *)url 72 method:(NSInteger)method 73 paramenters:(NSDictionary *)params 74 prepareExecute:(PrepareExecuteBlock)prepare 75 success:(SuccessBlock)success 76 failure:(FailureBlock)failure { 77 78 NSLog(@"请求网络地址为:%@",url); 79 80 if ([self isConnectionAvailable]) { 81 82 //预处理 83 if (prepare) { 84 prepare(); 85 } 86 87 switch (method) { 88 case HttpRequestGet: 89 [self.manager GET:url parameters:params progress:nil success:success failure:failure]; 90 break; 91 case HttpRequestPost: 92 [self.manager POST:url parameters:params progress:nil success:success failure:failure]; 93 break; 94 case HttpRequestPut: 95 [self.manager PUT:url parameters:params success:success failure:failure]; 96 break; 97 case HttpRequestDelete: 98 [self.manager DELETE:url parameters:params success:success failure:failure]; 99 break; 100 default: 101 break; 102 } 103 104 } else { 105 106 [self showExceptionDialog]; 107 } 108 } 109 110 - (BOOL)isConnectionAvailable { 111 112 return self.isConnect; 113 } 114 115 - (void)showExceptionDialog { 116 117 [[[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接异常,请检查网络连接" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil] show]; 118 } 119 120 @end
1 #import <UIKit/UIKit.h> 2 #import "UIImageView+WebCache.h" 3 4 typedef void (^DownloadSuccessBlock) (SDImageCacheType cacheType, UIImage *image); 5 typedef void (^DownloadFailureBlock) (NSError *error); 6 typedef void (^DownloadProgressBlock) (CGFloat progress); 7 8 @interface UIImageView (SDWebImage) 9 10 /** 11 * SDWebImage 下载并缓存图片 12 * 13 * @param url 图片的url 14 * 15 * @param place 还未下载成功时的替换图片 16 * 17 */ 18 - (void)downloadImage:(NSString *)url 19 place:(UIImage *)place; 20 21 /** 22 * SDWebImage 下载并缓存图片和下载进度 23 * 24 * @param url 图片的url 25 * 26 * @param place 还未下载成功时的替换图片 27 * 28 * @param success 图片下载成功 29 * 30 * @param failure 图片下载失败 31 * 32 * @param progress 图片下载进度 33 */ 34 - (void)downloadImage:(NSString *)url 35 place:(UIImage *)place 36 success:(DownloadSuccessBlock)success 37 failure:(DownloadFailureBlock)failure 38 received:(DownloadProgressBlock)progress; 39 40 41 42 @end
1 #import "UIImageView+SDWebImage.h" 2 3 @implementation UIImageView (SDWebImage) 4 5 - (void)downloadImage:(NSString *)url 6 place:(UIImage *)place 7 { 8 [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:place options:SDWebImageLowPriority | SDWebImageRetryFailed]; 9 } 10 11 12 - (void)downloadImage:(NSString *)url 13 place:(UIImage *)place 14 success:(DownloadSuccessBlock)success 15 failure:(DownloadFailureBlock)failure 16 received:(DownloadProgressBlock)progress 17 { 18 [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:place options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) { 19 20 progress((float)receivedSize/expectedSize); 21 22 } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 23 24 if (error) { 25 failure(error); 26 }else{ 27 // image是下载好的图片 28 self.image = image; 29 success(cacheType, image); 30 } 31 }]; 32 } 33 34 @end