简单block封装afnetworking
MMAFNetWorking.h
#import <Foundation/Foundation.h>
@class AFHTTPSessionManager;
/**
根据AFNetWorking定义三个block.
*/
typedef void(^success) (NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject);
typedef void(^failure) (NSURLSessionDataTask * _Nonnull task, id _Nullable error);
typedef void(^uploadProgress) (NSProgress *_Nonnull);
@interface MMAFNetWorking : NSObject
/**
把block弄成属性.这一步也可以省略.
*/
@property (nonatomic,copy) success _Nullable successBlock;
@property (nonatomic,copy) failure _Nullable failureBlock;
+ (instancetype _Nullable )sharedMMAFNetWorking;
- (NSURLSessionDataTask *_Nullable)POST:(NSString *_Nonnull)URLString
parameters:(id _Nullable )parameters
progress:(uploadProgress _Nullable )uploadProgress
success:(success _Nullable )success
failure:(failure _Nullable )failure;
- (NSURLSessionDataTask *_Nullable)GET:(NSString *_Nonnull)URLString
parameters:(id _Nullable )parameters
progress:(uploadProgress _Nullable )uploadProgress
success:(success _Nullable )success
failure:(failure _Nullable )failure;
@end
MMAFNetWorking.m
#import "MMAFNetWorking.h"
#import "AFNetworking.h"
@interface MMAFNetWorking()
@property (nonatomic,strong) AFHTTPSessionManager *afHSM;
@end
@implementation MMAFNetWorking
// 用来保存唯一的单例对象
static MMAFNetWorking *_instance;
/**
allocWithZone方法的重写是为了防止开发者在外面用此方法创建对象,因为alloc方法中会实现allocWithZone 这个方法,如果只重写alloc方法达不到效果。
*/
+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ //onceToken是GCD用来记录是否执行过 ,如果已经执行过就不再执行(保证执行一次)
_instance = [super allocWithZone:zone];
});
return _instance;
}
/**
dispatch_once
在整个程序的运行过程中,始终只执行一次
*/
+ (instancetype)sharedMMAFNetWorking
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
_instance.afHSM = [AFHTTPSessionManager manager];
_instance.afHSM.responseSerializer = [AFHTTPResponseSerializer serializer];
});
return _instance;
}
/**
copy相当于一个强指针,重写此方法可以防止创建一个新的对象,这样就是一个比较完美的单例模式了
*/
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
- (NSURLSessionDataTask *_Nullable)POST:(NSString *_Nonnull)URLString
parameters:(id _Nullable )parameters
progress:(uploadProgress _Nullable )uploadProgress
success:(success _Nullable )success
failure:(failure _Nullable )failure
{
return [self.afHSM POST:URLString parameters:parameters progress:uploadProgress success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(task,responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(task, error);
}
}];
}
- (NSURLSessionDataTask *_Nullable)GET:(NSString *)URLString parameters:(id)parameters progress:(uploadProgress)uploadProgress success:(success)success failure:(failure)failure
{
return [self.afHSM GET:URLString parameters:parameters progress:uploadProgress success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(task,responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(task, error);
}
}];
}
然后viewcontroller实现 方法。
#import "ViewController.h"
#import "MMAFNetWorking.h"
@interface ViewController ()
/**
node 建立的本地服务器
*/
@property (nonatomic,copy) NSString * url;
@property (nonatomic,strong) NSDictionary *dict;
/**
封装的类
*/
@property (nonatomic,strong) MMAFNetWorking *MMAFNW;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_url = @"http://127.0.0.1:3000/";
_MMAFNW = [MMAFNetWorking sharedMMAFNetWorking];
_dict = @{@"111":@"333"};
}
- (IBAction)postClick:(UIButton *)sender {
[_MMAFNW POST:_url parameters:_dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"成功成功====%@",dict);
} failure:^(NSURLSessionDataTask * _Nonnull task, id _Nullable error) {
NSLog(@"--------------error=%@",error);
}];
}
- (IBAction)getClick:(UIButton *)sender {
[_MMAFNW GET:_url parameters:_dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"成功成功====%@",dict);
} failure:^(NSURLSessionDataTask * _Nonnull task, id _Nullable error) {
NSLog(@"--------------error=%@",error);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
恩,很舒服。
点我查看Demo