AFNetworking 3.1, afnetworking3.1
I heard that the AFHttpWorking version may affect Apple's review. Today, the latest version of AFHttpWorking is released and simple encapsulation is made. Here I downloaded two tools through cocoapods.
1 = AFHttpWorking 2 = Why does JSONKit use jsonkit? touchjson is used for json parsing in the past. It was found that jsonkit is the most efficient in third-party json parsing, so today, I also took it down. Here I only use the get/post/network judgment methods for AFHttpWorking. Below I post code
. H file
#import <Foundation/Foundation.h>#import "AFDataDefine.h"#import <AFNetworking/AFNetworking.h>static NSString* const kAFAppDotNetAPIBaseURLString=@"http://www.xx.com";@interface APIClient : AFHTTPSessionManager+ (APIClient *)sharedClient;-(void)getUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback;-(void)postUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback;-(void)checkNetWorkingIsOrNoUse: (void (^)( int StateOrType))callback;@end
. M file
# Import "APIClient. h "# import <JSONKit/JSONKit. h> # pragma mark-@ implementation APIClient + (instancetype) sharedClient {static APIClient * _ sharedClient = nil; static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ sharedClient = [[APIClient alloc] initWithBaseURL: [NSURL URLWithString: kAFAppDotNetAPIBaseURLString]; _ sharedClient. securityPolicy = [AFSecurityPolicy policyWithPinningMode: AFSSLP InningModeCertificate]; _ sharedClient. requestSerializer. timeoutInterval = 20;}); _ sharedClient. responseSerializer. acceptableContentTypes = [NSSet setWithObjects: @ "text/html", @ "text/plain", @ "charset = UTF-8", @ "Content-Type ", @ "application/json", nil]; return _ sharedClient;} # pragma mark-/*** network monitoring (in what network status) ** @ callback 1 unknown Network * @ callback 2 No Network * @ callback 3 Cellular Data Network * @ callback 4 WiFi network */-(void) checkNe TWorkingIsOrNoUse: (void (^) (int StateOrType) callback {// create a network monitor [[AFNetworkReachabilityManager sharedManager] listener: ^ (AFNetworkReachabilityStatus status) {switch (status) {case failed: callback (1); break; case AFNetworkReachabilityStatusNotReachable: callback (2); break; case AFNetworkReachabilityStatusReachableViaWWAN: callback (3); brea K; case when: callback (4); break; default: break ;}}]; [AFNetworkReachabilityManager sharedManager] startMonitoring];}-(void) postUrl :( NSString *) URLString parameters :( id) parameters call :( void (^) (RespInfo * info) callback {// first, judge whether there is a network [self checkNetWorkingIsOrNoUse: ^ (int StateOrType) {if (StateOrType = 2) {RespInfo * retobj = [[RespInfo alloc] init]; retobj. MBSuccess = NO; NSString * AlertInfo = @ "check whether the network is smooth. "; Retobj. mMsg = AlertInfo; callback (retobj);} else {[self POST: URLString parameters: parameters progress: ^ (NSProgress * _ Nonnull uploadProgress) {[[UIApplication sharedApplication] callback: YES]; // enable the status bar animation} success: ^ (NSURLSessionDataTask * _ Nonnull task, id _ Nullable responseObject) {NSString * JsonStr = [responseObject JSONString]; NSDictionary * MyDic = [JsonStr objectFrom JSONString]; // declare the tired RespInfo * retobj = [[RespInfo alloc] init] returned by the block; // determine whether the returned result is successful NSString * IsOrNoSuccess = [MyDic objectForKey: @ "Success"]; if ([IsOrNoSuccess intValue] = 1) {retobj. mBSuccess = YES; // The returned type is determined here. If it is 0, the value of mObject is not obtained. Otherwise, int JudgeInt = [[MyDic objectForKey: @ "ControlType"] intValue] is used. if (JudgeInt = 0) {// only obtain the List Value NSString * DataListArrayStr = [MyDic objectForKey: @ "Rows"]; retobj. listData = [DataListArra YStr objectFromJSONString];} else if (JudgeInt = 1) // obtain the additional Dictionary value {NSString * addicationDic = [MyDic objectForKey: @ "AddicationDictionary"]; retobj. mObjectDictionary = [addicationDic objectFromJSONString];} else if (JudgeInt = 2) // both take {NSString * DataListArrayStr = [MyDic objectForKey: @ "Rows"]; retobj. listData = [DataListArrayStr objectFromJSONString]; NSString * addicationDic = [MyDic objectForKey: @ "AddicationDict Ionary "]; retobj. mObjectDictionary = [addicationDic objectFromJSONString];} // The returned public message NSString * AlertInfo = [MyDic objectForKey: @ "Msg"]; retobj. mMsg = AlertInfo;} else {retobj. mBSuccess = NO; NSString * AlertInfo = [MyDic objectForKey: @ "Msg"]; retobj. mMsg = AlertInfo;} callback (retobj);} failure: ^ (NSURLSessionDataTask * _ Nullable task, NSError * _ Nonnull error) {RespInfo * retobj = [[RespInfo alloc] init ]; Retobj. mBSuccess = NO; retobj. mMsg = error. domain;}] ;}}] ;}- (void) getUrl :( NSString *) URLString parameters :( id) parameters call :( void (^) (RespInfo * info )) callback {// first determine whether there is a network [self checkNetWorkingIsOrNoUse: ^ (int StateOrType) {if (StateOrType = 2) {RespInfo * retobj = [[RespInfo alloc] init]; retobj. mBSuccess = NO; NSString * AlertInfo = @ "check whether the network is smooth. "; Retobj. mMsg = AlertInfo; callback (retobj);} else {[self GET: URLString parameters: parameters progress: ^ (NSProgress * _ Nonnull downloadProgress) {[[UIApplication sharedApplication] callback: YES]; // enable the status bar animation} success: ^ (NSURLSessionDataTask * _ Nonnull task, id _ Nullable responseObject) {[[UIApplication sharedApplication] networkactivityindicatorvisible: NO]; NSString * JsonStr = [responseObject JSONString]; NSDictionary * MyDic = [JsonStr objectFromJSONString]; // declare the tired RespInfo * retobj = [[RespInfo alloc] init] returned by the block; // determine whether the returned result is successful NSString * IsOrNoSuccess = [MyDic objectForKey: @ "Success"]; if ([IsOrNoSuccess intValue] = 1) {retobj. mBSuccess = YES; // The returned type is determined here. If it is 0, the value of mObject is not obtained. Otherwise, int JudgeInt = [[MyDic objectForKey: @ "ControlType"] intValue] is used. if (JudgeInt = 0) {// only obtain the List Value NSString * DataListArrayStr = [MyDic objectForKey: @ "Rows"]; retobj. listData = [DataListArrayStr objectFromJSONString];} else if (JudgeInt = 1) // use the additional Dictionary value {NSString * addicationDic = [MyDic objectForKey: @ "AddicationDictionary"]; retobj. mObjectDictionary = [addicationDic objectFromJSONString];} else if (JudgeInt = 2) // both take {NSString * DataListArrayStr = [MyDic objectForKey: @ "Rows"]; retobj. listData = [DataListArrayStr objectFromJSONString]; NSString * addicationDic = [MyDic objectForKey: @ "AddicationDictionary"]; retobj. mObjectDictionary = [addicationDic objectFromJSONString];} // The returned public message NSString * AlertInfo = [MyDic objectForKey: @ "Msg"]; retobj. mMsg = AlertInfo;} else {retobj. mBSuccess = NO; NSString * AlertInfo = [MyDic objectForKey: @ "Msg"]; retobj. mMsg = AlertInfo;} callback (retobj);} failure: ^ (nsurlsessionask ask * _ Nullable task, NSError * _ Nonnull error) {RespInfo * retobj = [[RespInfo alloc] init]; retobj. mBSuccess = NO; retobj. mMsg = error. domain;}] ;}}] ;}// @ end
Class returned by the block
# Pragma mark-RespInfo outer encapsulation data @ interface RespInfo: AFDataDefine @ property (nonatomic, strong) NSArray * listData; // list data @ property (nonatomic, strong) NSDictionary * mObjectDictionary; // comes with the dictionary @ property (nonatomic, strong) NSString * mMsg; // message @ property (nonatomic, assign) BOOL mBSuccess; // returns the status, success or failure, determine this + (RespInfo *) infoWithError :( NSError *) error; + (RespInfo *) infoWithErrorMessage :( NSString *) errMsg; @ end
Call
1 #import "ViewController.h" 2 #import "APIClient.h" 3 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad {11 [super viewDidLoad];12 // Do any additional setup after loading the view, typically from a nib.13 NSUUID *iD=[[NSUUID alloc]init];14 NSString *UUIDvALUE=[iD UUIDString];15 16 17 18 19 20 NSDictionary *Dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"dataType", nil];21 22 [[APIClient sharedClient] getUrl:@"/Mobile/Jmfww.asmx/JmfwwCommunity" parameters:Dic call:^(RespInfo *info) {23 if ([info mBSuccess]) {24 25 26 27 28 }29 }];30 31 }32 33 - (void)didReceiveMemoryWarning {34 [super didReceiveMemoryWarning];35 // Dispose of any resources that can be recreated.36 }37 38 @end