网络请求工具是我们经常用到的工具类,下面用OC语言和Swift语言封装AFNetworking网络请求工具,废话不多说,直接上代码。
OC语言封装AFN网络请求工具
- 创建类NetworkTool,继承AFHTTPSessionManager
.h文件
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
@interface NetworkTool : AFHTTPSessionManager
//创建单例
+ (instancetype)sharedTool;
//网络请求方法
- (void)requestWithURL:(NSString *)url parameters: (NSDictionary *)parameters method: (NSString *)method callBack: (void(^)(id responseObject))callBack;
@end
.m文件--分两种方法实现
1.先来个普通的实现方法
#import "NetworkTool.h"
@implementation NetworkTool
//单例实现
+ (instancetype)sharedTool{
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
-(void)requestWithURL:(NSString *)url parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
//判断发送的是什么请求
//GET请求
if([method isEqualToString:@"GET"]) {
[self GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
callBack(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
//POST请求
if ([method isEqualToString:@"POST"]) {
[self POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
callBack(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
}
@end
2.参考到AFN内部POST,GET等请求都是用同一个方法实现:dataTaskWithHTTPMethod,我们来封装一下
- 由于AFN内部的dataTaskWithHTTPMethod方法时私有的,我们如果不做处理在这里无法使用的
- 如何使用父类的私有方法?---利用OC运行时,当发送消息调用方法时在本类中找不到该方法,则会去父类中调用
#import "NetworkTool.h"
//1.创建协议,声明协议方法,把AFN内部的实现方法复制过来
@protocol OCNetworkToolProtocol <NSObject>
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;
@end
//2.遵守协议
@interface NetworkTool()<OCNetworkToolProtocol>
@end
//然后就可以调用了
@implementation NetworkTool
//单例实现
+ (instancetype)sharedTool{
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
-(void)requestWithURL:(NSString *)url parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
//如果使用父类的dataTask方法(由于是父类的私有方法,所以调用不了,这时要使用代理),则不用判断
NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:method URLString:url parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
callBack(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];
Swift语言封装AFN网络请求工具
- 创建类继承AFHTTPSessionManager
import Foundation
class SwiftNetworkTool: AFHTTPSessionManager {
//创建单例
static let shared = SwiftNetworkTool()
//网络工具方法
func reqeust(url: String, paramaters: [String: Any]? = nil, method: String, callBack:@escaping (_ responseObject:Any?)->()){
//抽取请求成功和失败的闭包
let success = {
(task: URLSessionDataTask, responseObject: Any?) in callBack(responseObject)
}
let failure = {
(task: URLSessionDataTask?,error: Error) in
callBack(nil)
}
//GET请求网络数据
if method == "GET" {
self.get(url, parameters: paramaters, progress: nil, success: success, failure: failure)
}
//POST请求网络数据
if method == "POST" {
self.post(url, parameters: paramaters, progress: nil, success: success, failure: failure)
}
}
}
网友评论