对请求参数、请求头和AFNetworking本身进行了封装
- ViewController显示用法
//需要打开网络请求开关,并导入AFNetworking第三方库
//具体封装内容全在BSGHTTPManager里面,使用封装的时候直接复制过去即可使用
BSGHTTPManager * manager = [BSGHTTPManager manager];
//本请求不需要参数,以下展示参数写法:
manager.phone = @"示例";
[manager BSGPost:@"http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json" success:^(NSURLSessionDataTask *task, id responseDictionary) {
NSLog(@"responseDictionary:%@",responseDictionary);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"error:%@",error);
}];
- BSGHTTPManager展示封装过程
- BSGHTTPManager.h
#import <Foundation/Foundation.h>
@interface BSGHTTPManager : NSObject
//参数
/** 手机号码,即账号*/
@property(nonatomic, copy) NSString * phone;
/** 密码*/
@property(nonatomic, copy) NSString * password;
//创建管理者
+(instancetype)manager;
//成功返回Block
typedef void(^successBlock)(NSURLSessionDataTask * task, id responseDictionary);
//失败返回Block
typedef void(^failureBlock)(NSURLSessionDataTask * task, NSError * error);
//POST方法
-(void)BSGPost:(NSString *)URLString success:(successBlock)success failure:(failureBlock)failture;
//GET方法
-(void)BSGGet:(NSString *)URLString success:(successBlock)success failure:(failureBlock)failture;
//Block写在内部的例子
-(void)BSGTest:(NSString *)aString andBlock:(void(^)(NSString *resultString))block;
@end
- BSGHTTPManager.m
#import "BSGHTTPManager.h"
#import <AFHTTPSessionManager.h>
@implementation BSGHTTPManager
+(instancetype)manager{
return [[[self class] alloc] init];
}
//AFNetworking的POST方法
-(void)BSGPost:(NSString *)URLString success:(successBlock)success failure:(failureBlock)failture
{
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
//内部设置请求头
[manager.requestSerializer setValue:@"2" forHTTPHeaderField:@"type"];
[manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"application"];
NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
paramDic = [self BSGGetParam];
[manager POST:URLString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//回传Block数据
success(task,responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//回传Block数据
failture(task,error);
}];
}
//AFNetworking的GET方法
-(void)BSGGet:(NSString *)URLString success:(successBlock)success failure:(failureBlock)failture
{
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
//内部设置请求头
[manager.requestSerializer setValue:@"2" forHTTPHeaderField:@"type"];
[manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"application"];
NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
paramDic = [self BSGGetParam];
[manager GET:URLString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//回传Block数据
success(task,responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//回传Block数据
failture(task,error);
}];
}
//获取网络请求参数。请求时直接将参数传入
-(NSMutableDictionary *)BSGGetParam{
NSMutableDictionary * tmpDic = [NSMutableDictionary dictionary];
tmpDic = [self BSGAddObjectFrom:tmpDic value:self.phone key:@"phone"];
tmpDic = [self BSGAddObjectFrom:tmpDic value:self.password key:@"password"];
// tmpDic = [self BSGAddObjectFrom:tmpDic value:self.<#value#> key:@"<#content#>"];
return tmpDic;
}
/* 判断加入请求参数的数据是否为空,如果为空则不添加,避免添加空元素报错*/
-(NSMutableDictionary *)BSGAddObjectFrom:(NSMutableDictionary *)anDictionary value:(NSString*)anValue key:(NSString *)anKey{
if (anValue) {
[anDictionary setObject:anValue forKey:anKey];
}
return anDictionary;
}
//测试示例
-(void)BSGTest:(NSString *)aString andBlock:(void (^)(NSString *))block
{
NSString * resultStr = [aString stringByAppendingString:@"123"];
block(resultStr);
}
@end