AFNetworking

Architecture( 结构 )
NSURLConnection
AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager
NSURLSession (iOS 7 / Mac OS X 10.9)
AFURLSessionManager
AFHTTPSessionManager
AFURLRequestSerialization
AFHTTPRequestSerializer
AFJSONRequestSerializer
AFPropertyListRequestSerializer
AFURLResponseSerialization
AFHTTPResponseSerializer
AFJSONResponseSerializer
AFXMLParserResponseSerializer
AFXMLDocumentResponseSerializer (Mac OS X)
AFPropertyListResponseSerializer
AFImageResponseSerializer
AFCompoundResponseSerializer
Additional Functionality
AFSecurityPolicy
AFNetworkReachabilityManager

详细解释如下:

NSURLConnection 组件 (iOS 6 & 7)

AFURLConnectionOperation - NSOperation 的子类,负责管理 NSURLConnection 并且实现其 delegate 方法。
AFHTTPRequestOperation - AFURLConnectionOperation 的子类,用于生成 HTTP 请求,可以区别可接受的和不可接受的状态码及内容类型。2.0 版本中的最大区别是,你可以直接使用这个类,而不用继承它,原因可以在“序列化”一节中找到。
AFHTTPRequestOperationManager - 包装常见 HTTP web 服务操作的类,通过 AFHTTPRequestOperation 由NSURLConnection 支持。


NSURLSession 组件 (iOS 7)


AFURLSessionManager - 创建、管理基于 NSURLSessionConfiguration 对象的 NSURLSession 对象的类,也可以管理 session 的数据、下载/上传任务,实现 session 和其相关联的任务的 delegate 方法。因为 NSURLSession API 设计中奇怪的空缺,任何和 NSURLSession 相关的代码都可以用 AFURLSessionManager 改善。
AFHTTPSessionManager - AFURLSessionManager 的子类,包装常见的 HTTP web 服务操作,通过 AFURLSessionManager 由NSURLSession 支持。
总的来说:为了支持新的 NSURLSession API 以及旧的未弃用且还有用的 NSURLConnection,AFNetworking 2.0 的核心组件分成了 request operation 和 session 任务。AFHTTPRequestOperationManager 和 AFHTTPSessionManager 提供类似的功能,在需要的时候(比如在 iOS 6 和 7 之间转换),它们的接口可以相对容易的互换。
之前所有绑定在 AFHTTPClient的功能,比如序列化、安全性、可达性,被拆分成几个独立的模块,可被基于NSURLSession 和 NSURLConnection 的 API 使用。


Serialization


AFNetworking 2.0 新构架的突破之一是使用序列化来创建请求、解析响应。可以通过序列化的灵活设计将更多业务逻辑转移到网络层,并更容易定制之前内置的默认行为。
<AFURLRequestSerializer> - 符合这个协议的对象用于处理请求,它将请求参数转换为 query string 或是 entity body 的形式,并设置必要的 header。那些不喜欢 AFHTTPClient 使用 query string 编码参数的家伙,你们一定喜欢这个。
<AFURLResponseSerializer> - 符合这个协议的对象用于验证、序列化响应及相关数据,转换为有用的形式,比如 JSON 对象、图像、甚至基于 Mantle 的模型对象。相比没完没了地继承 AFHTTPClient,现在 AFHTTPRequestOperation 有一个responseSerializer 属性,用于设置合适的 handler。同样的,再也没有没用的受 NSURLProtocol 启发的 request operation 类注册,取而代之的还是很棒的 responseSerializer 属性。谢天谢地。

Security&Reachability


AFSecurityPolicy - 评估服务器对安全连接针对指定的固定证书或公共密钥的信任。tl;dr 将你的服务器证书添加到 app bundle,以帮助防止 中间人攻击。
可达性
从 AFHTTPClient 解藕的另一个功能是网络可达性。现在你可以直接使用它,或者使用 AFHTTPRequestOperationManager /AFHTTPSessionManager 的属性。
AFNetworkReachabilityManager - 这个类监控当前网络的可达性,提供回调 block 和 notificaiton,在可达性变化时调用。
实时性
AFEventSource - EventSource DOM API 的 Objective-C 实现。建立一个到某主机的持久 HTTP 连接,可以将事件传输到事件源并派发到听众。传输到事件源的消息的格式为 JSON Patch 文件,并被翻译成 AFJSONPatchOperation 对象的数组。可以将这些 patch operation 应用到之前从服务器获取的持久性数据集。

UIKit Extension


之前 AFNetworking 中的所有 UIKit category 都被保留并增强,还增加了一些新的 category。
AFNetworkActivityIndicatorManager:在请求操作开始、停止加载时,自动开始、停止状态栏上的网络活动指示图标。
UIImageView+AFNetworking:增加了 imageResponseSerializer 属性,可以轻松地让远程加载到 image view 上的图像自动调整大小或应用滤镜。比如,AFCoreImageSerializer 可以在 response 的图像显示之前应用 Core Image filter。
UIButton+AFNetworking (新):与 UIImageView+AFNetworking 类似,从远程资源加载 image 和 backgroundImage。
UIActivityIndicatorView+AFNetworking (新):根据指定的请求操作和会话任务的状态自动开始、停止UIActivityIndicatorView。
UIProgressView+AFNetworking (新):自动跟踪某个请求或会话任务的上传/下载进度。
UIWebView+AFNetworking (新): 为加载 URL 请求提供了更强大的API,支持进度回调和内容转换。


Difference Between 2.0 & 1.0

iOS自带的请求网络的方法为NSURLConnection,后来改进版本NSURLSession

NSURLConnection、NSMutableURLRequest、NSURLRequest使用方法

简单说就是使用NSMutableURLRequest或者NSURLRequest创建一个request,使用NSURLConnection执行这个request

AFNetworking2.0相对于1.0的改进,主要也是增加了对NSURLSession的支持,以及一些逻辑分离,减少耦合性。

原理

1.0使用方法

2.0Example  && 2.0使用方法

2.0简介

1.0&2.0移植Guide


Usage & Debug(使用方法)

Debug时候一般只需要打印出结果或者错误信息进行调试。下面列举一下可能用到的错误信息:

CFNetWorkErrors


1.AFHTTPRequestOperationManager 将通用的与服务器应用交互的操作封装了起来,包括了请求的创建,回复的序列化,网络指示器的监测,安全性等。

#1 Get请求

API.h

#import "AFHTTPRequestOperationManager.h"

typedef void (^JSONResponseBlock)(NSDictionary* json);

@interface API__AFHTTPRequestOperationManager : AFHTTPRequestOperationManager

+(API__AFHTTPRequestOperationManager*)sharedInstance;
-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock;

@end

API.m

#import "API_ AFHTTPRequestOperationManager.h"

@implementation API__AFHTTPRequestOperationManager

+(API__AFHTTPRequestOperationManager*)sharedInstance
{
    static API__AFHTTPRequestOperationManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[API__AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8090/instagram"]];
        sharedInstance.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    });
    return sharedInstance;
}

-(void)commandWithParams:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock
{
    [[API__AFHTTPRequestOperationManager sharedInstance] GET:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        completionBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];
}
@end
ViewController.m

-(void)HROMgetData
{
    [[API__AFHTTPRequestOperationManager sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"login", @"command", @"test",@"password", nil] onCompletion:^(NSDictionary *json) {
        //completion
        if (![json objectForKey:@"error"]) {
            //success
            [[[UIAlertView alloc]initWithTitle:@"Success!" message:@"Your photo is uploaded" delegate:nil cancelButtonTitle:@"Yay!" otherButtonTitles: nil] show];
            NSLog(@"JSON: %@",json);
            
        } else {
            //error, check for expired session and if so - authorize the user
            [[[UIAlertView alloc]initWithTitle:@"Error!" message:[json objectForKey:@"error"] delegate:nil cancelButtonTitle:@"Oh NO!" otherButtonTitles: nil] show];
        }
    }];
}

#2.Post请求

将commandWithParams中manager的Get请求改成Post即可

#3.Multi-Part Request(复杂的post请求)

主要是传输图片

API.m

-(void)commandWithParams:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock
{
    NSURL* uploadFile = nil;
    if ([params objectForKey:@"file"]) {
        uploadFile = [NSURL fileURLWithPath:[params objectForKey:@"file"]];
        [params removeObjectForKey:@"file"];
    }
    [[API__AFHTTPRequestOperationManager sharedInstance] POST:@"" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:uploadFile name:@"pic" fileName:@"kiss.jpg" mimeType:@"image/jpeg" error:nil];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        completionBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];
}
ViewController.m
-(void)HROMUploadWithMultiPart
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kiss" ofType:@"jpg"];
    [[API__AFHTTPRequestOperationManager alloc] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"login", @"command", @"test",@"password",filePath, @"file",nil] onCompletion:^(NSDictionary *json) {
        if (![json objectForKey:@"error"]) {
            //success
            [[[UIAlertView alloc]initWithTitle:@"Success!" message:@"Your photo is uploaded" delegate:nil cancelButtonTitle:@"Yay!" otherButtonTitles: nil] show];
            NSLog(@"JSON: %@",json);
            
        } else {
            //error, check for expired session and if so - authorize the user
            [[[UIAlertView alloc]initWithTitle:@"Error!" message:[json objectForKey:@"error"] delegate:nil cancelButtonTitle:@"Oh NO!" otherButtonTitles: nil] show];
        }
    }];
}

2.AFURLSessionManager

API.h

#import "AFURLSessionManager.h"

typedef void (^JSONResponseBlock)(NSDictionary* json);

@interface API_AFURLSessionManager : AFURLSessionManager

+(API_AFURLSessionManager*)sharedInstance;
-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock;

@end

API.m

#import "API_AFURLSessionManager.h"

@implementation API_AFURLSessionManager

+(API_AFURLSessionManager*)sharedInstance
{
    static API_AFURLSessionManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[API_AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        sharedInstance.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    });
    
    return sharedInstance;
}

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock {
    
}
@end

ViewController.m

#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "API_AFURLSessionManager.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self getData];
}

- (void)getData
{
//    [self DataTask];
//    [self Upload];
//    [self Download];
//    [self UploadWithMultiPart];
}

-(void)DataTask
{
    NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDataTask *dataTask = [[API_AFURLSessionManager sharedInstance] dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@",error);
        }else{
            NSLog(@"response: %@ \n responseObject: %@",response,responseObject);
        }
    }];
    [dataTask resume];
}

-(void)Upload
{
    NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"kiss" ofType:@"jpg"];
    NSURL *filePath = [NSURL fileURLWithPath:filePathString];
    NSURLSessionUploadTask *uploadTask = [[API_AFURLSessionManager sharedInstance] uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSLog(@"Success: %@ %@", response, responseObject);
        }
    }];
    [uploadTask resume];
}

-(void)Download
{
    NSURL *URL = [NSURL URLWithString:@"http://d1xzuxjlafny7l.cloudfront.net/downloads/PromoTest.zip"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
                  
    NSURLSessionDownloadTask *downloadTask = [[API_AFURLSessionManager sharedInstance] downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager]URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"File downloaded to: %@", filePath);
    }];
    [downloadTask resume];
}

-(void)UploadWithMultiPart
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"kiss" ofType:@"jpg"];
    NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
//    UIImage* image = [[UIImage alloc]initWithContentsOfFile:filePath];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"upload", @"command", @"test", @"title", nil];
    
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost:8090/instagram" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:fileNameAndPath name:@"file" fileName:@"kiss.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];
    
    NSProgress *progress = nil;
    NSURLSessionUploadTask *uploadTask = [[API_AFURLSessionManager sharedInstance] uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@",error);
        }else{
            NSLog(@"response: %@ \n responseObject: %@",response,responseObject);
        }
    }];
    [uploadTask resume];
}
@end



Instagram

http://www.raywenderlich.com/13511/how-to-create-an-app-like-instagram-with-a-web-service-backend-part-12


参考文献

http://www.cnblogs.com/fuhaots2009/archive/2013/12/16/3476390.html

http://blog.csdn.net/u013089311/article/details/38900559



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值