AFNetworking 3.0请求代码示例

最近做一个微博登录时,集成了AFNetworking 3.1 , 发现AFN 3比原来的2.0语法简单了好多,就拿我的登录模块的其中一段代码说明:

AFNetworking 2.0 版本的请求数据代码:
NSString *path = @"https://api.weibo.com/oauth2/access_token";
NSString *params = [NSString stringWithFormat:@"client_id=%@&client_secret=%@&grant_type=authorization_code&code=%@&redirect_uri=%@",kAppKey,kAppSecret,code,kAppURI];
NSLog(@"%@",path);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
    callback(dic);
    NSLog(@"登陆成功 token=%@",[dic objectForKey:@"access_token"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"登陆失败");
}];
[operation start];

--------------------------------------------------------------------
AFNetworking 3.0的请求数据代码是这样的:

//创建会话管理者
AFHTTPSessionManager *sessionManage = [AFHTTPSessionManager manager];

//创建URL的参数相关字典
NSDictionary *parameterDic = @{
                           @"client_id" : kAppKey,
                           @"client_secret" : kAppSecret,
                           @"grant_type" : @"authorization_code",
                           @"code" : code,
                           @"redirect_uri" : kAppURI,
                           };

//请求网络数据
[sessionManage POST:@"https://api.weibo.com/oauth2/access_token" parameters:parameterDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    WBLog(@"Login success! responseClass:%@ -- responseObject: %@", [responseObject class], responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    WBLog(@"Login Fail!Erro: %@", error);
}];

你可能感兴趣的:(AFNetworking 3.0请求代码示例)