通常当用到第三方框架的时候,我们都会将其封装成一个工具类来使用,以便在将来更换框架的时候方便修改,以下为封装后的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];
}];
}
好了,大概就是这么多,仅供参考,文中会用到一些其他的类,就不放上去了。