Ios开发对第三方框架AFNetWorking网络访问的封装使用

        通常当用到第三方框架的时候,我们都会将其封装成一个工具类来使用,以便在将来更换框架的时候方便修改,以下为封装后的AFNetWorking的使用工具类HttpManager,记录一下,以方便今后使用,对于具体的需求应该相应的进行变化,例如在下载图片时会有所不同。


HttpManager.h文件内容为:


#import <Foundation/Foundation.h>


typedef  void (^HttpSuccessBlock) (id JSON);
typedef  void (^HttpFailureBlock) (NSError *error);


@interface HttpManager : NSObject
+ (void)getWithPath:(NSString *)baseUrl  path:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure;


+ (void)postWithPath:(NSString *)baseUrl  path:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure;
@end

        

HttpManager.m文件的内容为:


#import "HttpManager.h"

#import "AFNetworking.h"
#import "config.h"



@implementation HttpManager


#pragma mark - baseUrl 为访问的基路径如:https://api.weibo.com,path是跟在基路径之后的部分路径,如:oauth2/access_token(因为AFNetworking的访问方式才这样划分),params是参数,success  是访问成功时调用的block,failure是访问失败时调用的bolcok,method为确定发送的是post还是get请求


+ (void)requestWithPath:(NSString *)baseUrl  path:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success  failure:(HttpFailureBlock)failure  method:(NSString *)method
{
    // 1.创建post请求
    AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:baseUrl  ]];
    
    NSMutableDictionary *allParams = [NSMutableDictionary dictionary];
    // 拼接传进来的参数
    if (params) {
        [allParams setDictionary:params];
    }


     // 拼接token参数,这一部分可以根据实际需要来删除,因为本代码段取至新浪微博的开发代码,所以就保留了
    NSString *token = [AccountTool sharedAccountTool].account.accessToken;
    if (token) {
        [allParams setObject:token forKey:@"access_token"];
    }


    NSURLRequest *post = [client requestWithMethod:method path:path parameters:allParams];
    
    // 2.创建AFJSONRequestOperation对象
    NSOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:post
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if (success == nil) return;
        success(JSON);
    }
    failure : ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        if (failure == nil) return;
        failure(error);
    }];
    
    // 3.发送请求
    [op start];
}


//发送post请求
+ (void)postWithPath:(NSString *)baseUrl  path:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure
{
    [self requestWithPath:baseUrl  path:path params:params success:success failure:failure method:@"POST"];
}


//发送get请求
+ (void)getWithPath:(NSString *)baseUrl  path:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure
{
    [self requestWithPath:baseUrl  path:path params:params success:success failure:failure method:@"GET"];
}
@end



在controller中对该封装类的调用如下,应该根据具体需求来变化其中的代码:

#pragma mark 换取accessToken
- (void)getAccessToken:(NSString *)requestToken
{
    [HttpManager  postWithPath:@"https://api.weibo.com"  path:@"oauth2/access_token" params:@{
         @"client_id" : kAppKey,
         @"client_secret" : kAppSecret,
         @"grant_type" : @"authorization_code",
         @"redirect_uri" : kRedirectURI,
         @"code" : requestToken
     } success:^(id JSON) {
         // 保存账号信息
         Account *account = [[Account alloc] init];
         account.accessToken = JSON[@"access_token"];
         account.uid = JSON[@"uid"];
         [[AccountTool sharedAccountTool] saveAccount:account];
         
         // 回到主页面
         self.view.window.rootViewController = [[MainController alloc] init];
         
         // 清除指示器
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
     } failure:^(NSError *error) {
         // 清除指示器
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
     }];
}



     好了,大概就是这么多,仅供参考,文中会用到一些其他的类,就不放上去了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值