Can be used to login, registration, change the password, to obtain a mobile phone verification code.
First import AFN third-party libraries
NetWorkTool.h
networktool.h//Network Request////Created by Ding Yunfei on 15/11/8.//copyright©2015 year DLS. All rights reserved.//#import <Foundation/Foundation.h> #import "AFNetworking.h" @protocol networktooldelegate <NSObject> @optional/************************ Get the phone verification code *****************teg = = 1*/-(void) requestsucceedgetdata :(Nsdictionary *) dict;//successfully obtained cell phone verification code call dict return data-(void) requestfailure;//failed when calling/************************ login ************ Teg = = 2*/-(void) Loginsucceedgetdata: (nsdictionary *) dict;//login succeeded-(void) loginfailure;//Login failed/********* Register *************************teg = = 3*/-(void) Registsucceedgetdata: (nsdictionary *) dict;//registration sent a successful call;-( void) registfailure;//registration failed;/*********************** Change Password **********************teg = = 4*/-(void) Updataseccretsucceedgetdata: (nsdictionary *) dict;//change Request sent successfully-(void) updataseccretfailure;//failed @end@interface Networktool:nsobject{afhttprequestoperationmanager *_managerr; Afnetworkreachabilitymanager *_afnetworkreach;} @property (nonatomic, Strong) Id<networktooldelegate>delegate;+ (Networktool *) sharenetworkjiexi;/** *post for login, registration, change password */#pragma mark--Proxy value-(void) Postrequestwithurl: (NSString *) urlstr withdict: (nsdictionary *) dict Withinteger: (nsinteger) teg;-(void) Getrequestwithurl: (NSString *) urlstr withdict: (nsdictionary *) dict; #pragma mark--The Block form--(void) Postrequestwithurl: (NSString *) urlstr withdict: (nsdictionary *) dict Success: (void (^) (id data)) SUCCESSD Failure: (void (^) (Nserror *err)) failure;-(void) Getrequestwithurl: (NSString *) urlstr wlthdict: (nsdictionary *) dict Success: (void (^) (ID data)) successed Failure: (void (^) (nserror *err)) Failure; @end
networktool.m
Created by Ding Yunfei on 15/11/8.//copyright©2015 year DLS. All rights reserved.//#import "NetWorkTool.h" #import "AFNetworking.h" @interface networktool ()//@property (nonatomic, Retain) Afhttprequestoperationmanager *managerr, @end @implementation networktool+ (Networktool *) shareNetWorkJieXi{ Static Networktool *sumbloo; Static dispatch_once_t Oncesumbtoken; Dispatch_once (&oncesumbtoken, ^{sumbloo = [[Networktool alloc] init]; }); return Sumbloo;} -(ID) init{self = [super init]; if (self) {_managerr = [Afhttprequestoperationmanager manager]; _managerr.requestserializer = [Afhttprequestserializer serializer]; _managerr.responseserializer = [Afhttpresponseserializer serializer]; Managerr.responseSerializer.acceptableContentTypes = [Nsset setwithobjects:@ "Text/plain", @ "Image/jpeg", @ "text/ HTML ", nil]; _afnetworkreach = [Afnetworkreachabilitymanager Sharedmanager]; } return self;} #pragma mark--Proxy value/** * POST Request * * @parAM URLSTR Interface Address * @param dict dictionary * @param teg distinguish different requests */-(void) Postrequestwithurl: (NSString *) urlstr withdict: (nsdict Ionary *) dict Withinteger: (nsinteger) teg{//Open thread [self start]; [_managerr post:urlstr parameters:dict success:^ (afhttprequestoperation *operation, id responseObject) {Nsdictionary *jiexidic = [Nsjsonserialization jsonobjectwithdata:responseobject Options:nsjsonreadingmutableconta Iners Error:nil]; Nsdictionary *dict=[jiexidic objectforkey:@ "Data"]; Switch (TEG) {Case 1: [Self.delegate requestsucceedgetdata:dict];//get Verification Code success break ; Case 2: [Self.delegate loginsucceedgetdata:dict];//Landing succeeded break; Case 3: [self.delegate registsucceedgetdata:dict];//registration successful break; Case 4: [Self.delegate updataseccretsucceedgetdata:dict];//Change password successfully default:break; } } failure:^ (Afhttprequestoperation *operation, nserror *error) {switch (TEG) {Case 1: [Self.delegate Requestfailure]; Break Case 2: [Self.delegate loginfailure]; Break Case 3: [Self.delegate registfailure]; Break Case 4: [Self.delegate updataseccretfailure]; Default:break; } }]; }/* *get */-(void) Getrequestwithurl: (NSString *) urlstr withdict:(nsdictionary *) dict{[_managerr GET:urlStr parameters :D ICT success:^ (afhttprequestoperation *operation, id responseobject) {nsdictionary *jiexidic = [NSJSONSerializati On Jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers Error:nil]; Nsdictionary *dict=[jiexidic objectforkey:@ "Data"]; [Self.delegate requestsucceedgetdata:dict];//obtain Verification code success//[Self.delegate Loginsucceedgetdata:dict];//Login Successful///[Self.delegate registsucceedgetdata:dict];//Registration Successful/// [Self.delegate updataseccretsucceedgetdata:dict];//Change Password succeeded} failure:^ (Afhttprequestoperation *operati On, Nserror *error) {[Self.delegate requestfailure]; [Self.delegate loginfailure];//[Self.delegate registfailure];////[SELF.D Elegate Updataseccretfailure]; }];} #pragma mark-Block pass value//post-(void) Postrequestwithurl: (NSString *) urlstr withdict: (nsdictionary *) dict Success: (void (^) (ID data)) SUCCESSD Failure: (void (^) (Nserror *)) failure{[_managerr post:urlstr parameters:dict success:^ ( Afhttprequestoperation *operation, id responseobject) {SUCCESSD (responseobject); } failure:^ (Afhttprequestoperation *operation, Nserror *error) {failure (error); }];} get-(void) Getrequestwithurl: (NSString *) urlstr wlthdict: (nsdictionary *) dict Success:(void (^) (id)) successed Failure: (void (^) (Nserror *)) failure{[_managerr get:urlstr parameters:dict success:^ (afhttpre Questoperation *operation, id responseobject) {successed (responseobject); } failure:^ (Afhttprequestoperation *operation, Nserror *error) {failure (error); }];} -(void) start{if ([[[_managerr Operationqueue] issuspended]) {[[_managerr operationqueue] setsuspended:no]; }} @end
There is insufficient support please point out.
AFN simple two-time package