為大家分享一個(gè)IOS處理網(wǎng)絡(luò)請(qǐng)求,網(wǎng)絡(luò)上傳,網(wǎng)絡(luò)下載等功能全面的一個(gè)第三方框架-AFNetworking,這是一個(gè)使用非常方便的網(wǎng)絡(luò)框架.
最新的版本是基于NSURLSession,原來的NSURLConnectionOperation在此已經(jīng)移除.
簡(jiǎn)介
AFNetworking is a delightful networking library for iOS and Mac OS X. It's built on top of the Foundation URL Loading System, extending the powerful high-level networking abstractions built into Cocoa. It has a modular architecture with well-designed, feature-rich APIs that are a joy to use.
Perhaps the most important feature of all, however, is the amazing community of developers who use and contribute to AFNetworking every day. AFNetworking powers some of the most popular and critically-acclaimed apps on the iPhone, iPad, and Mac.
Choose AFNetworking for your next project, or migrate over your existing projects—you'll be happy you did!
翻譯:AFNetworking是適用于IOS和MacOS的一個(gè)輕量級(jí)的網(wǎng)絡(luò)庫.它搭建于URL加載系統(tǒng)的框架之上,擴(kuò)展了有效的高級(jí)網(wǎng)絡(luò)提取并內(nèi)置了Cocoa.它提供了設(shè)計(jì)優(yōu)秀的模塊化體系,方便使用的豐富的API.
最大的特點(diǎn)是每天有驚人數(shù)量的社區(qū)開發(fā)者在為AFNetworking做出貢獻(xiàn). AFNetworking在一些最受歡迎的IPhone,IPad,Mac上的APP都有使用它.
選擇AFNetworking在您的下一個(gè)項(xiàng)目中,或者移植到你現(xiàn)存的項(xiàng)目上---你將會(huì)很樂意于使用它!
大體架構(gòu)
網(wǎng)絡(luò)管理類
AFURLSessionManager
AFHTTPSessionManager
請(qǐng)求
<AFURLRequestSerialization>
AFHTTPRequestSerializer
AFJSONRequestSerializer
AFPropertyListRequestSerializer
響應(yīng)
<AFURLResponseSerialization>
AFHTTPResponseSerializer
AFJSONResponseSerializer
AFXMLParserResponseSerializer
AFXMLDocumentResponseSerializer (Mac OS X)
AFPropertyListResponseSerializer
AFImageResponseSerializer
AFCompoundResponseSerializer
AFHTTPSessionManager是AFURLSessionManager的子類,一般推薦使用AFHTTPSessionManager.
在這里我只為大家介紹下封裝成工具類的操作,以GET網(wǎng)絡(luò)請(qǐng)求為例,其他的方法是類似使用的,在項(xiàng)目中大量的使用,這是非常必要的.
創(chuàng)建.h文件
#import <Foundation/Foundation.h>
//因?yàn)槲沂鞘褂胮ods導(dǎo)入的.所以這里是導(dǎo)入庫文件的形式
#import <AFNetworking.h>
@interface NetTools : AFHTTPSessionManager
+(instancetype)shareNetTools;
-(void)GETURLString:(NSString *)URLString andSuccessBlock:(void(^)(id success))successBlock andErrorBlock:(void(^)(NSError *error))errorBlock;
@end
創(chuàng)建.m文件
#import "NetTools.h"
@implementation NetTools
//設(shè)計(jì)成單例
+(instancetype)shareNetTools
{
static NetTools* instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//設(shè)置baseURL.
NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/"];
instance = [[NetTools alloc] initWithBaseURL:url];
//解決反序列問題
instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
});
return instance;
}
//這里是是get請(qǐng)求方法演示,其他的方法類似.
-(void)GETURLString:(NSString *)URLString andSuccessBlock:(void(^)(id success))successBlock andErrorBlock:(void(^)(NSError *error))errorBlock
{
//NSLog(@"當(dāng)前的請(qǐng)求地址:%@", URLString);
[self GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (successBlock) {
successBlock(responseObject);
}
NSLog(@"請(qǐng)求成功^");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (errorBlock) {
NSLog(@"下載sh%@", error);
errorBlock(error);
}
}];
}
@end