可用于登陆,注册,修改密码,获得手机验证码。
首先导入AFN第三方库
NetWorkTool.h
// NetWorkTool.h // 网络请求 // // Created by 丁云飞 on 15/11/8. // Copyright © 2015年 DLS. All rights reserved. // #import <Foundation/Foundation.h> #import "AFNetworking.h" @protocol netWorkToolDelegate<NSObject> @optional /************************获得手机验证码*****************teg == 1*/ -(void)requestSucceedGetData:(NSDictionary *)dict;//成功获得手机验证码调用dict返回数据 -(void)requestFailure;//失败时调用 /************************登陆*************************teg == 2*/ -(void)loginSucceedGetData:(NSDictionary *)dict;//登陆成功 -(void)loginFailure;//登陆失败 /***********************注册*************************teg == 3*/ -(void)registSucceedGetData:(NSDictionary *)dict;//注册发送成功调用; -(void)registFailure;//注册失败; /***********************修改密码**********************teg == 4*/ -(void)updataSeccretSucceedGetData:(NSDictionary *)dict;//修改请求发送成功过 -(void)updataSeccretFailure;//失败 @end @interface NetWorkTool : NSObject { AFHTTPRequestOperationManager *_managerr; AFNetworkReachabilityManager *_afNetworkReach; } @property (nonatomic, strong)id<netWorkToolDelegate>delegate; + (NetWorkTool *)shareNetWorkJieXi; /** *POST用于登陆,注册,修改密码 */ #pragma mark -- 代理传值 -(void)PostRequestWithUrl:(NSString *)urlStr WithDict:(NSDictionary *)dict WithInteger:(NSInteger)teg; -(void)GETRequestWithUrl:(NSString *)urlStr WithDict:(NSDictionary *)dict; #pragma mark -- block形式传值 -(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 丁云飞 on 15/11/8. // Copyright © 2015年 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 -- 代理传值 /** * POST请求 * * @param urlStr 接口地址 * @param dict 字典 * @param teg 区分不同请求 */ -(void)PostRequestWithUrl:(NSString *)urlStr WithDict:(NSDictionary *)dict WithInteger:(NSInteger)teg{ //开启线程 [self start]; [_managerr POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *jiexiDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; NSDictionary *dict=[jiexiDic objectForKey:@"data"]; switch (teg) { case 1: [self.delegate requestSucceedGetData:dict];//获得验证码成功 break; case 2: [self.delegate loginSucceedGetData:dict];//登陆成功 break; case 3: [self.delegate registSucceedGetData:dict];//注册成功 break; case 4: [self.delegate updataSeccretSucceedGetData:dict];//修改密码成功 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:dict success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *jiexiDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; NSDictionary *dict=[jiexiDic objectForKey:@"data"]; [self.delegate requestSucceedGetData:dict];//获得验证码成功 // [self.delegate loginSucceedGetData:dict];//登陆成功 // // [self.delegate registSucceedGetData:dict];//注册成功 // // [self.delegate updataSeccretSucceedGetData:dict];//修改密码成功 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [self.delegate requestFailure]; // [self.delegate loginFailure]; // [self.delegate registFailure]; // // [self.delegate updataSeccretFailure]; }]; } #pragma mark -- block传值 //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:^(AFHTTPRequestOperation *operation, id responseObject) { successed(responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { failure(error); }]; } -(void)start{ if ([[_managerr operationQueue] isSuspended]) { [[_managerr operationQueue] setSuspended:NO]; } } @end
有不足支持请大家指出。