IOS project-related secondary encapsulation of @ AFN & amp; SDWeb, ios Project @ afn

Source: Internet
Author: User

IOS project-related secondary encapsulation of @ AFN & SDWeb, ios Project @ afn

I. AFNetworking and SDWebImge are powerful and common third parties. However, in actual applications, they need to be encapsulated for reuse. Today I will share with you the second encapsulation of AFN & SDWeb.

1.

HttpClient. h and. m

. H defines four common http get post put delete requests

Generally, they are used for addition, deletion, and query respectively.

Define the pre-processing, the request is successfully processed, and the block that the request fails to process

And method declaration with multiple parameters w

. M sets the request type and response type, and whether the listener network has a network. If not, a network exception warning box is displayed.

2.

UIImageView + WebCache. h and. m

This is a classification method that encapsulates network request images.

. H defines the block for successful loading, loading failure, and loading progress.

The method of loading the preset image and the method of requesting to download the image on the network when the download fails.

. M

Is the specific implementation of the two methods

1 # import <Foundation/Foundation. h> 2 # import "AFNetworking. h "3 4 // HTTP request category 5 typedef NS_ENUM (NSInteger, HttpRequestType) {6 HttpRequestGet, 7 HttpRequestPost, 8 HttpRequestPut, 9 HttpRequestDelete, 10 }; 11 12 13/** 14 * pre-processing block15 */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: NSObject23 24 + (HttpClient *) defaultClient; 25 26/** 27 * HTTP Request (GET, POST, PUT, DELETE) 28*29 * @ param url request address 30 * @ param method Request type 31 * @ param params request parameter 32 * @ param prepare pre-processing 33 * @ param success the request is successfully processed 34 * @ param failure request failure handling 35 */36 37-(void) failed :( NSString *) url38 method :( NSInteger) method39 paramenters :( NSDictionary *) params40 failed :( partial) prepare41 success :( SuccessBlock) success42 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]; 19 // set the request type to 20 self. manager. requestSerializer = [AFHTTPRequestSerializer serializer]; 21 // set the response type 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 // enable listening 27 [self openNetMonitoring]; 28 29} 30 return self; 31} 32 33-(void) openNetMonitoring {34 35 [[AFNetworkReachabilityManager sharedManager] statuses: ^ (AFNetworkReachabilityStatus status) {36 37 switch (status) {38 case when: 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 (@ "request network address: % @ ", url); 79 80 if ([self isConnectionAvailable]) {81 82 // preprocessing 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: @ "prompt" message: @ "network connection exception, check the network connection "delegate: nil cancelButtonTitle: @" OK "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 download and cache image 12*13 * @ param url url14 * 15 * @ param place replacement image 16*17 */ 18-(void) downloadImage :( NSString *) url19 place :( UIImage *) place; 20 21/** 22 * SDWebImage: Download and cache images and download progress 23*24 * @ param url: url25 * 26 * @ param place: replace image 27*28 * @ param success image download succeeded 29*30 * @ param failure image download failed 31*32 * @ param progress image download progress 33 */34-(void) downloadImage :( NSString *) url35 place :( UIImage *) place36 success :( DownloadSuccessBlock) success37 failure :( DownloadFailureBlock) failure38 received :( DownloadProgressBlock) progress; 39 40 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 | priority]; 9} 10 11 12-(void) downloadImage :( NSString *) url13 place :( UIImage *) place14 success :( DownloadSuccessBlock) success15 failure :( failed) failure16 received :( DownloadProgressBlock) progress17 {18 [self defined: [NSURL URLWithString: url] placeholderImage: place options: enabled | invalid 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 is the downloaded image 28 self. image = image; 29 success (cacheType, image); 30} 31}]; 32} 33 34 @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.