• AFN的简单二次封装


    可用于登陆,注册,修改密码,获得手机验证码。

    首先导入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
    

     有不足支持请大家指出。

  • 相关阅读:
    正则表达式
    CSS常用格式
    数据库原理第十章考试形式
    汇编语言(王爽第三版) 实验5编写、调试具体多个段的程序
    7-10 多项式A除以B (25分)(多项式除法)
    C. Air Conditioner(区间交集)
    X的因子链(分解质因数,)
    AtCoder Beginner Contest 155 E.Payment
    cf584DDima and Lisa(素数性质,三素数,哥德巴赫猜想)
    CF1207C Gas Pipeline(DP)
  • 原文地址:https://www.cnblogs.com/DLS520/p/5017887.html
Copyright © 2020-2023  润新知