目录:
1、为什么要用AFNetworking
2、AFNetworking的用法
一、为什么要用AFNetworking
在ios开发中,一般情况下,简单的向某个web站点简单的页面提交请求并获取服务器的响应,用xcode自带的NSURLConnection是能胜任的。但是,在绝大部分下我们所需要访问的web页面则是属于那种受到权限保护的页面,并不是有一个简单的URL可以访问的。这就涉及到了Session和Cookie的处理了,在此时使用NSURLConnection也是能够达到要求的,只是其中处理起来的复杂度和难度就提升了。
扩展:1、Session:中文有译作时域的,就是只某个客户端在访问服务器起到停止访问这一段的时间间隔被称为时域。
2、Cookie:由服务器发送给客服端,把Cookie的key:value值储存在本地文件夹下,当下次请求的时候能够直接发送Cookie获得权限验证
二、AFNetworking的用法
1、提交GET请求和提交POST请求 AFNetworking是第三方的框架,所以需要开发者自行下载,安装。并在AFNetworking.h文件导入#import“AFHTTPRequestOpeartionManager.h ”,把AFNetworking.h头文件放入prefix文件中。
b、根据服务器内容的不同,为AFHTTPRequestOpeartionManger对象指定不同的解析器,该对象默认的解析器是JSON和Plist文件解析器。如果服务器的数据是XML格式则需要手动的更改解析器
c、发送GET请求: 用Manager对象调用 GET:parameters:success:failure:方法即可,success代码块和failue代码块在网络请求成功/失败过后调用。
d、success:参数指定了代码块中处理服务器响应成功的正确数据,failue:参数指定了代码块中处理服务器响应失败的错误数据、
AFHTTPRquestOperationManager
包含了常见的HTTP访问web站点的模式,有创建请求,连续的响应,网络类型监视以及安全。
Get请求
<span style="font-size:12px;">
//创建AFHTTPRequestOperationManager对象
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManger manager];
//调用get方法
[manager GET:@“http://example.com/resources.json”parameters : parameters
//加载成功的代码块,可以接收数据
success:^(AFHTTPRequestOperation *operation,id responseobject)]{
NSLog(@“json“:%@”,responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error){
NSLog(@“Error:%@”,error);
}];
Post请求
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Post多个请求
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
创立下载任务
AFURLSessionManager创建并完善了一个NSURLSession的对象基于遵从NSURLSessionDelegate与NSURLSessionDataDelegate协议NSURLSessionConfigration对象。
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaul
tSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionC
onfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:re
quest progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
{
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDir
ectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response sugg
estedFilename]];