網(wǎng)絡(luò)請求工具是我們經(jīng)常用到的工具類贼陶,下面用OC語言和Swift語言封裝AFNetworking網(wǎng)絡(luò)請求工具钞馁,廢話不多說巍杈,直接上代碼坪稽。
OC語言封裝AFN網(wǎng)絡(luò)請求工具
- 創(chuàng)建類NetworkTool曼玩,繼承AFHTTPSessionManager
.h文件
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
@interface NetworkTool : AFHTTPSessionManager
//創(chuàng)建單例
+ (instancetype)sharedTool;
//網(wǎng)絡(luò)請求方法
- (void)requestWithURL:(NSString *)url parameters: (NSDictionary *)parameters method: (NSString *)method callBack: (void(^)(id responseObject))callBack;
@end
.m文件--分兩種方法實(shí)現(xiàn)
1.先來個普通的實(shí)現(xiàn)方法
#import "NetworkTool.h"
@implementation NetworkTool
//單例實(shí)現(xiàn)
+ (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{
//判斷發(fā)送的是什么請求
//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內(nèi)部POST,GET等請求都是用同一個方法實(shí)現(xiàn):dataTaskWithHTTPMethod,我們來封裝一下
- 由于AFN內(nèi)部的dataTaskWithHTTPMethod方法時私有的窒百,我們?nèi)绻蛔鎏幚碓谶@里無法使用的
- 如何使用父類的私有方法黍判?---利用OC運(yùn)行時,當(dāng)發(fā)送消息調(diào)用方法時在本類中找不到該方法篙梢,則會去父類中調(diào)用
#import "NetworkTool.h"
//1.創(chuàng)建協(xié)議顷帖,聲明協(xié)議方法,把AFN內(nèi)部的實(shí)現(xiàn)方法復(fù)制過來
@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.遵守協(xié)議
@interface NetworkTool()<OCNetworkToolProtocol>
@end
//然后就可以調(diào)用了
@implementation NetworkTool
//單例實(shí)現(xiàn)
+ (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方法(由于是父類的私有方法渤滞,所以調(diào)用不了贬墩,這時要使用代理),則不用判斷
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網(wǎng)絡(luò)請求工具
- 創(chuàng)建類繼承AFHTTPSessionManager
import Foundation
class SwiftNetworkTool: AFHTTPSessionManager {
//創(chuàng)建單例
static let shared = SwiftNetworkTool()
//網(wǎng)絡(luò)工具方法
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請求網(wǎng)絡(luò)數(shù)據(jù)
if method == "GET" {
self.get(url, parameters: paramaters, progress: nil, success: success, failure: failure)
}
//POST請求網(wǎng)絡(luò)數(shù)據(jù)
if method == "POST" {
self.post(url, parameters: paramaters, progress: nil, success: success, failure: failure)
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者