AFNetworking 3.1
听说之后AFHttpWorking版本可能会影响到苹果的审核,今天下了最新版本的AFHttpWorking,并且做了简单的封装,我这里是通过cocoapods下载了两个工具
1=AFHttpWorking 2=JSONKit 为什么要下jsonkit勒,以前json解析都使用touchjson,偶然发现资料说jsonkit效率是第三方json解析中最高的,所以今天把它也下了来下,我这里只做了AFHttpWorking 的get/post/网络判断三个方法。下面我贴代码
.h文件
#import <Foundation/Foundation.h>
#import "AFDataDefine.h"
#import <AFNetworking/AFNetworking.h>
static NSString* const kAFAppDotNetAPIBaseURLString=@"http://www.xx.com";
@interface APIClient : AFHTTPSessionManager
+ (APIClient *)sharedClient;
-(void)getUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback;
-(void)postUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback;
-(void)checkNetWorkingIsOrNoUse: (void (^)( int StateOrType))callback;
@end
.m文件
#import "APIClient.h"
#import <JSONKit/JSONKit.h> #pragma mark - @implementation APIClient
+ (instancetype)sharedClient {
static APIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFAppDotNetAPIBaseURLString]];
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
_sharedClient.requestSerializer.timeoutInterval =20; }); _sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"charset=UTF-8",@"Content-Type",@"application/json",nil];; return _sharedClient;
} #pragma mark - /**
* 网络监测(在什么网络状态)
*
* @callback 1 未知网络
* @callback 2 无网络
* @callback 3 蜂窝数据网
* @callback 4 WiFi网络
*/ -(void)checkNetWorkingIsOrNoUse: (void (^)( int StateOrType))callback
{
// 创建网络监测者 [ [AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status)
{
case AFNetworkReachabilityStatusUnknown:
callback(1);
break; case AFNetworkReachabilityStatusNotReachable:
callback(2);
break; case AFNetworkReachabilityStatusReachableViaWWAN:
callback(3); break;
case AFNetworkReachabilityStatusReachableViaWiFi:
callback(4);
break; default: break;
}
}] ; [[AFNetworkReachabilityManager sharedManager] startMonitoring]; } -(void)postUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback
{ //首先判断是否有网络
[self checkNetWorkingIsOrNoUse:^(int StateOrType) {
if (StateOrType==2) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
NSString *AlertInfo=@"请检查网络是否畅通。";
retobj.mMsg=AlertInfo;
callback(retobj); } else
{ [self POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 开启状态栏动画 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *JsonStr=[responseObject JSONString];
NSDictionary *MyDic=[JsonStr objectFromJSONString];
//申明block返回的累
RespInfo* retobj = [[RespInfo alloc]init];
//判断返回是否成功
NSString *IsOrNoSuccess=[MyDic objectForKey:@"Success"];
if ([IsOrNoSuccess intValue]==1) {
retobj.mBSuccess=YES;
//这里判断返回类型,如果为0不去取mObject的值,否则要取
int JudgeInt=[[MyDic objectForKey:@"ControlType"]intValue];
if (JudgeInt==0) {//只取列表值 NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; }
else if(JudgeInt==1)//字取附加字典值
{
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
else if(JudgeInt==2)//两个都取
{ NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
//返回的公用提示消息
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; }
else
{ retobj.mBSuccess=NO; NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; } callback(retobj); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO; retobj.mMsg=error.domain; }]; }
}]; } -(void)getUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback
{
//首先判断是否有网络
[self checkNetWorkingIsOrNoUse:^(int StateOrType) {
if (StateOrType==2) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
NSString *AlertInfo=@"请检查网络是否畅通。";
retobj.mMsg=AlertInfo;
callback(retobj); } else
{ [self GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 开启状态栏动画 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; NSString *JsonStr=[responseObject JSONString];
NSDictionary *MyDic=[JsonStr objectFromJSONString];
//申明block返回的累
RespInfo* retobj = [[RespInfo alloc]init];
//判断返回是否成功
NSString *IsOrNoSuccess=[MyDic objectForKey:@"Success"];
if ([IsOrNoSuccess intValue]==1) {
retobj.mBSuccess=YES;
//这里判断返回类型,如果为0不去取mObject的值,否则要取
int JudgeInt=[[MyDic objectForKey:@"ControlType"]intValue];
if (JudgeInt==0) {//只取列表值 NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; }
else if(JudgeInt==1)//字取附加字典值
{
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
else if(JudgeInt==2)//两个都取
{ NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString]; NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
//返回的公用提示消息
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; }
else
{ retobj.mBSuccess=NO; NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo; } callback(retobj);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO; retobj.mMsg=error.domain; }]; }
}]; } // @end
block返回的类
#pragma mark - RespInfo 外层封装数据
@interface RespInfo : AFDataDefine
@property (nonatomic,strong) NSArray *listData; //列表数据
@property(nonatomic,strong)NSDictionary *mObjectDictionary;//附带字典
@property (nonatomic,strong) NSString* mMsg; //提示消息
@property (nonatomic,assign) BOOL mBSuccess; //返回状态,成功失败,判断这个 +(RespInfo *)infoWithError:(NSError *)error;
+(RespInfo *)infoWithErrorMessage:(NSString *)errMsg; @end
调用
#import "ViewController.h"
#import "APIClient.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUUID *iD=[[NSUUID alloc]init];
NSString *UUIDvALUE=[iD UUIDString]; NSDictionary *Dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"",@"dataType", nil]; [[APIClient sharedClient] getUrl:@"/Mobile/Jmfww.asmx/JmfwwCommunity" parameters:Dic call:^(RespInfo *info) {
if ([info mBSuccess]) { }
}]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
本人创业做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。

AFNetworking 3.1的更多相关文章
- 【原】AFNetworking源码阅读(六)
[原]AFNetworking源码阅读(六) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这一篇的想讲的,一个就是分析一下AFSecurityPolicy文件,看看AF ...
- 【原】AFNetworking源码阅读(五)
[原]AFNetworking源码阅读(五) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中提及到了Multipart Request的构建方法- [AFHTTP ...
- 【原】AFNetworking源码阅读(四)
[原]AFNetworking源码阅读(四) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇还遗留了很多问题,包括AFURLSessionManagerTaskDe ...
- 【原】AFNetworking源码阅读(三)
[原]AFNetworking源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇的话,主要是讲了如何通过构建一个request来生成一个data tas ...
- 【原】AFNetworking源码阅读(二)
[原]AFNetworking源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中我们在iOS Example代码中提到了AFHTTPSessionMa ...
- 【原】AFNetworking源码阅读(一)
[原]AFNetworking源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 AFNetworking版本:3.0.4 由于我平常并没有经常使用AFNetw ...
- AFNetworking 3.0 源码解读 总结(干货)(下)
承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...
- AFNetworking 3.0 源码解读 总结(干货)(上)
养成记笔记的习惯,对于一个软件工程师来说,我觉得很重要.记得在知乎上看到过一个问题,说是人类最大的缺点是什么?我个人觉得记忆算是一个缺点.它就像时间一样,会自己消散. 前言 终于写完了 AFNetwo ...
- AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking
AFNetworking的源码解读马上就结束了,这一篇应该算是倒数第二篇,下一篇会是对AFNetworking中的技术点进行总结. 前言 上一篇我们总结了 UIActivityIndicatorVie ...
- AFNetworking 3.0 源码解读(十)之 UIActivityIndicatorView/UIRefreshControl/UIImageView + AFNetworking
我们应该看到过很多类似这样的例子:某个控件拥有加载网络图片的能力.但这究竟是怎么做到的呢?看完这篇文章就明白了. 前言 这篇我们会介绍 AFNetworking 中的3个UIKit中的分类.UIAct ...
随机推荐
- Atitit.eclipse 4.3 4.4 4.5 4.6新特性
Atitit intellij idea的使用总结attilax 1. ideaIC-2016.2.4.exe1 1.1. Ij vs eclipse市场份额1 1.2. Ij的优点(方便的支持gro ...
- 浅谈iOS版本号
作者:Travis FIR.im 一直在尽量兼容不同使用习惯的版本号形式, 但是在使用中我们发现好多开发者对怎么更好的用版本号来标示应用很陌生. 这是篇基础文章, 简单介绍 iOS 的版本号. 名词解 ...
- Spring注意事项(各部分理解)
(1),每一个bean属性,就是一个普通的java类. 类有属性,有方法,如何交给容器管理.(注解的方式,xml方式配置) (2),通过Bean来实例化对象的方式 1.通过构造器(一般是无参的默认构造 ...
- 分享10条Visual Studio 2012的开发使用技巧
使用Visual Studio 2012有一段时间了,并不是追赶潮流,而是被逼迫无可奈何.客户要求的ASP.NET MVC 4的项目,要用.NET 4.5来运行.经过一段时间的摸索,得到一点经验和体会 ...
- SVN更改登录用户
如果装了TortoiseSVN: Settings -> Saved Data -> Authentication Data -> clear.即可清除保存的上个用户登录信息:当再次 ...
- Surface Normal Vector in OpenCascade
Surface Normal Vector in OpenCascade eryar@163.com 摘要Abstract:表面上某一点的法向量(Normal Vector)指的是在该点处与表面垂直的 ...
- Uvaoj10054 - The Necklace
/* 题意:打印欧拉回路! 思路:开始时不明白,dfs为什么是后序遍历? 因为欧拉回路本身是一条回路,那么我们在dfs时,可能存在提前找到回路,这条回路可能不是欧拉回路, 因为没有遍历完成所有的边!如 ...
- Yii的学习(5)--Active Record的关联
官网原文:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.arr 官网中后半段为英文,而且中文的内容比英文少一些,先放到这里,之后有时 ...
- PHP 字符串函数
字符串是字符序列,比如 "Hello world!". PHP 字符串函数 在本节中,我们将学习常用的字符串操作函数. PHP strlen() 函数 strlen() 函数返回字 ...
- 网络通信之Socket与LocalSocket的比较
Socket与LocalSocket都可以实现网络通信,两个有什么区别呢? LocalSocket其通信方式与Socket差不多,只是LocalSocket没有跨越网络边界. 于是,思考到一个问题:a ...