From the Apple system to bring the request class, to asihttprequest third-party request class, and then to afnetworking third-party request class, currently as long as the data request, is basically used AFN, so, here is purely recorded:
HRequestTools.h
#import<Foundation/Foundation.h>/** * Network request type*/typedef ns_enum (nsuinteger,httprequesttype) {/** * GET request*/Httprequesttypeget=1, /** * POST request*/Httprequesttypepost=2};@interfaceHrequesttools:nsobject/** * send a network request * * @param the URL string of the urlstring request * @param the parameter of the parameters request * @param type of request * @param succes The result of the S request*/+ (void) Requestwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Type: (httprequesttype) Type success: (void(^) (IDresponseobject)) Success Failure: (void(^) (Nserror *error)) Failure
Hrequesttools.m
#pragmaThe request method of the Mark Package + (void) Requestwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Type: (httprequesttype) Type success: (void(^) (IDresponseobject)) Success Failure: (void(^) (Nserror *error)) failure{Afhttpsessionmanager*manager =[Afhttpsessionmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; Manager.requestSerializer.timeoutInterval=30.0f; Switch(type) { CaseHttprequesttypeget: {[manager get:urlstring parameters:parameters success:^ (Nsurlsessiondatatask *task,IDresponseobject) { if(Success) {IDJsons =[nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers Error:nil]; Success (jsons); }} failure:^ (Nsurlsessiondatatask *task, Nserror *error) { if(failure) {failure (error); } }]; } Break; Casehttprequesttypepost: {if(Internets) {//when there is a networkAfhttpsessionmanager *manager =[Afhttpsessionmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; [Manager post:urlstring parameters:parameters success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) { if(Success) {IDJsons =[nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers Error:nil]; Success (jsons); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) { if(failure) {failure (error); [Htools Showtextonlyhud:@"The data request failed, please try again. "Delay2.0]; } }]; }Else //when there is no network{[Htools Showtextonlyhud:@"Please check your network"Delay2.0]; Nserror*error =Nil; Failure (error); } } Break; default: Break; } }
If you want to add a cache in the request, you can use this third party with Yycache,
About Yycache, you can recommend a few links: Http://www.jianshu.com/p/b8dcf6634fab
iOS development--use afnetworking 3.1.0, simple Request encapsulation class