为大家分享一个IOS处理网络请求,网络上传,网络下载等功能全面的一个第三方框架-AFNetworking,这是一个使用非常方便的网络框架.

最新的版本是基于NSURLSession,原来的NSURLConnectionOperation在此已经移除.

git链接请点击此处

简介

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的一个轻量级的网络库.它搭建于URL加载系统的框架之上,扩展了有效的高级网络提取并内置了Cocoa.它提供了设计优秀的模块化体系,方便使用的丰富的API.

最大的特点是每天有惊人数量的社区开发者在为AFNetworking做出贡献. AFNetworking在一些最受欢迎的IPhone,IPad,Mac上的APP都有使用它.

选择AFNetworking在您的下一个项目中,或者移植到你现存的项目上---你将会很乐意于使用它!

大体架构

网络管理类

AFURLSessionManager

AFHTTPSessionManager

请求

AFHTTPRequestSerializer
AFJSONRequestSerializer
AFPropertyListRequestSerializer

响应

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

AFHTTPSessionManager是AFURLSessionManager的子类,一般推荐使用AFHTTPSessionManager.

在这里我只为大家介绍下封装成工具类的操作,以GET网络请求为例,其他的方法是类似使用的,在项目中大量的使用,这是非常必要的.

创建.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

iOS | AFNetworking封装的更多相关文章

  1. IOS AFNETWORKING POST

    IOS AFNETWORKING POST 请求 #pragma mark post 请求 // 获取 url 路劲,不带参数 NSString *requestUrl = [[url compone ...

  2. iOS - AFNetworking 网络请求

    前言 在 iOS 开发中,一般情况下,简单的向某个 Web 站点简单的页面提交请求并获取服务器的响应,用 Xcode 自带的 NSURLConnection 是能胜任的.但是,在绝大部分下我们所需要访 ...

  3. iOS AFNetworking的使用

    转:http://www.cnblogs.com/lookenwu/p/3927897.html AFNetworking几乎是iOS上最常用的HTTP库了,AFNetworking也确实用起来简单, ...

  4. IOS AFNetworking简介及使用

    转:http://www.it165.net/pro/html/201405/13099.html 一AFNetworking简介AFNetworking是一个在IOS开发中使用非常多网络开源库,适用 ...

  5. iOS AFNetWorking源码详解(一)

    来源:Yuzeyang 链接:http://zeeyang.com/2016/02/21/AFNetWorking-one/ 首先来介绍下AFNetWorking,官方介绍如下: AFNetworki ...

  6. AFNetworking封装思路简析

    http://blog.csdn.net/qq_34101611/article/details/51698473 一.AFNetworking的发展 1. AFN 1.0版本 AFN 的基础部分是 ...

  7. AFNetworking封装-项目使用

    本篇博客是接着上一篇AFNetworking源码解析的后续,如果想对AFNetworking源码有所了解. 请读一下https://www.cnblogs.com/guohai-stronger/p/ ...

  8. iOS 自己封装的网络请求,json解析的类

    基本上所有的APP都会涉及网络这块,不管是用AFNetWorking还是自己写的http请求,整个网络框架的搭建很重要. 楼主封装的网络请求类,包括自己写的http请求和AFNetWorking的请求 ...

  9. [iOS AFNetworking框架实现HTTP请求、多文件图片上传下载]

    简单的JSON的HTTP传输就不说了,看一个简单的DEMO吧. 主要明白parameters是所填参数,类型是字典型.我把这部分代码封装起来了,以便多次调用.也许写在一起更清楚点. #pragma m ...

随机推荐

  1. [LeetCode]27. Remove Element移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  2. HTML <frameset> 标签

    <frameset></frameset>:框架标签,可以将页面分割,被frameset标签分割的页面,不允许使用body标签;frameset标签页面内只能出现framese ...

  3. 常用css属性记录

    CSS常用属性: 字体属性:(font)大小 font-size: x-large;(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD样式 font-style: ...

  4. PHP基础--strtr和str_replace字符替换函数

    (一)strtr是字符替换函数 (1)单个字符替换: <?php echo strtr("abba", "ab", "10"),&qu ...

  5. matlab练习程序(模糊集图像增强)

    算法有很多变种.不过主要就是以下三步. 1.设计隶属度函数将图像从空间域变换到模糊集域. 2.设计模糊增强算子,在模糊集域对图像进行处理. 3.根据第1步的隶属度函数重新将图像从模糊集域变换到空间域. ...

  6. 一个sql server 实施工程师的反思

    自14年开始从事数据库实施,至今(2018-02-16)晃眼间已经四个年头过去了,工作上的能力要求多数能自己解决,可这不应该成为我学习路上的终点.加之总觉得自己对sql 的理解有些浮于表面,所以借着春 ...

  7. 【Leetcode】【Easy】Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  8. 【Leetcode】【Easy】Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. Intel® Manager for Lustre* software(一)

    Intel® Manager for Lustre* software Installation 软件安装指导目录: 安装IML(Intel® Manager for Lustre* software ...

  10. Vim中 ctags 跳转直接跳到第一个匹配行的问题

    意图 用ctags搜索代码时, 用 ctrl + ] 后,只有一个匹配项直接跳转,有多个则列出所有匹配项选择跳转 问题 在 vim 中使用 ctags 是一个很令人舒服的事情,但有时一些默认的配置和不 ...