iOS | AFNetworking封裝

爲你們分享一個IOS處理網絡請求,網絡上傳,網絡下載等功能全面的一個第三方框架-AFNetworking,這是一個使用很是方便的網絡框架.
最新的版本是基於NSURLSession,原來的NSURLConnectionOperation在此已經移除.
git連接請點擊此處javascript

簡介

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.html

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.java

Choose AFNetworking for your next project, or migrate over your existing projects—you'll be happy you did!git

翻譯:AFNetworking是適用於IOS和MacOS的一個輕量級的網絡庫.它搭建於URL加載系統的框架之上,擴展了有效的高級網絡提取並內置了Cocoa.它提供了設計優秀的模塊化體系,方便使用的豐富的API.
最大的特色是天天有驚人數量的社區開發者在爲AFNetworking作出貢獻. AFNetworking在一些最受歡迎的IPhone,IPad,Mac上的APP都有使用它.
選擇AFNetworking在您的下一個項目中,或者移植到你現存的項目上---你將會很樂意於使用它!github

大致架構

網絡管理類

AFURLSessionManager
AFHTTPSessionManagerjson

請求


AFHTTPRequestSerializer
AFJSONRequestSerializer
AFPropertyListRequestSerializer
網絡

響應


AFHTTPResponseSerializer
AFJSONResponseSerializer
AFXMLParserResponseSerializer
AFXMLDocumentResponseSerializer (Mac OS X)
AFPropertyListResponseSerializer
AFImageResponseSerializer
AFCompoundResponseSerializer
架構

AFHTTPSessionManager是AFURLSessionManager的子類,通常推薦使用AFHTTPSessionManager.
在這裏我只爲你們介紹下封裝成工具類的操做,以GET網絡請求爲例,其餘的方法是相似使用的,在項目中大量的使用,這是很是必要的.app

建立.h文件
#import <Foundation/Foundation.h>
//由於我是使用pods導入的.因此這裏是導入庫文件的形式
#import <AFNetworking.h>

@interface NetTools : AFHTTPSessionManager

+(instancetype)shareNetTools;


-(void)GETURLString:(NSString *)URLString andSuccessBlock:(void(^)(id success))successBlock andErrorBlock:(void(^)(NSError *error))errorBlock;

@end

建立.m文件框架

#import "NetTools.h"

@implementation NetTools
//設計成單例
+(instancetype)shareNetTools
{
    static NetTools* instance;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        //設置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請求方法演示,其餘的方法相似.
-(void)GETURLString:(NSString *)URLString andSuccessBlock:(void(^)(id success))successBlock andErrorBlock:(void(^)(NSError *error))errorBlock
{
    //NSLog(@"當前的請求地址:%@", URLString);

    [self GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
        NSLog(@"請求成功^");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        if (errorBlock) {
            NSLog(@"下載sh%@", error);
            errorBlock(error);
        }
    }];
}

@end
相關文章
相關標籤/搜索