1.导入AFNetworking
2.创建封装类
创建FBYHomeService类继承NSObject .h代码2
#import@interface FBYHomeService : NSObject
//pageNum和action标记参数,可以区分接口类型等
//Alldic为网络请求报文
//url接口地址
//success获取接口成功返回参数
//failure网络请求失败错误信息
- (void)searchMessage:(NSString *)pageNum andWithAction:(NSString *)action andWithDic:(NSDictionary *)Alldic andUrl:(NSString *)url andSuccess:(void(^)(NSDictionary *dic))success andFailure:(void(^)(int fail))failure;
@end
.m代码
#import "FBYHomeService.h"
#import "AFNetworking.h"
@implementation FBYHomeService
- (void)searchMessage:(NSString *)pageNum andWithAction:(NSString *)action andWithDic:(NSDictionary *)Alldic andUrl:(NSString *)url andSuccess:(void (^)(NSDictionary *))success andFailure:(void (^)(int))failure{
//1.创建ADHTTPSESSIONMANGER对象
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
//2.设置该对象返回类型
[manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
if ([pageNum isEqualToString:[NSString stringWithFormat:@"get"]]) {
NSString *urlstr= [NSString stringWithFormat:@"%@%@",AGREEHanBingURL,url];
NSLog(@"%@",urlstr);
//调出请求头
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//将token封装入请求头
NSUserDefaults *tokenid = [NSUserDefaults standardUserDefaults];
NSString *token = [tokenid objectForKey:@"tokenid"];
NSLog(@"%@",token);
[manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
[manager GET:urlstr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dic = responseObject;
success(dic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}else {
NSString *urlstr= [NSString stringWithFormat:@"%@%@",AGREENewFuWuURL,url];
NSLog(@"%@",urlstr);
[manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"x-signature"];
[manager.requestSerializer setValue:@"2" forHTTPHeaderField:@"x-timestamp"];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:urlstr parameters:Alldic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dic = responseObject;
success(dic);
}failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(404);
}];
}
}
@end
3.封装类的使用
LoginViewController.m中代码 首先引入头文件
#import "AFNetworking.h"
#import "FBYHomeService.h"
//使用代码
NSMutableDictionary *mutdic=[NSMutableDictionary dictionaryWithCapacity:0];
mutdic[@"userName"] = @"admin";
mutdic[@"password"] = @"admin";
//网络请求
FBYHomeService *service1 = [[FBYHomeService alloc]init];
[service1 searchMessage:@"post" andWithAction:nil andWithDic:mutdic andUrl:MerchantLogin andSuccess:^(NSDictionary *dic) {
NSLog(@"%@",dic);
NSString *str = [dic objectForKey:@"code"];
int intString = [str intValue];
if (intString == 0) {
NSString *token = [dic objectForKey:@"token"];
NSLog(@"%@",token);
NSUserDefaults *tokenid = [NSUserDefaults standardUserDefaults];
[tokenid setObject:token forKey:@"tokenid"];
[tokenid synchronize];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action1];
[self presentViewController:alert animated:YES completion:nil];
}
} andFailure:^(int fail) {
}];
网友评论