为防止项目污染,另外考虑到今后项目的更新维护,便于网络请求框架的切换,通常需要将AFN封装一个类使用
Swift下:
import UIKit
import AFNetworking
enum MethodType: String {
case POST = "POST"
case GET = "GET"
}
class JSNetworkTool: AFHTTPSessionManager {
//创建网络单例对象
static let sharedNetworkTool: JSNetworkTool = {
let tool = JSNetworkTool()
tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
return tool
}()
//网络请求公共方法
func request(requestMethod: MethodType, urlString: String, parameters: AnyObject?, success: (response: AnyObject?)->(), failure: (error: NSError)->()) -> Void {
if requestMethod == MethodType.GET {
//GET请求
self.GET(urlString, parameters: parameters, progress: { progress in
//进度
}, success: { (NSURLSessionDataTask, response) in
//请求成功回调
success(response: response)
}, failure: { (NSURLSessionDataTask, error) in
//请求失败回调
failure(error: error)
})
}else{
//POST请求
self.POST(urlString, parameters: parameters, progress: { progress in
//进度
}, success: { (NSURLSessionDataTask, response) in
//请求成功回调
success(response: response)
}, failure: { (NSURLSessionDataTask, error) in
//请求失败回调
failure(error: error)
})
}
}
}
使用演示
extension JSNetworkTool {
func oauthLoadUserAccount(code: String, success: (response: AnyObject?)->(), failure: (error: NSError)->() ) -> Void {
let urlString = "https://api.weibo.com/oauth2/access_token"
let parameters = [
"client_id":APPKEY,
"client_secret":APPSECRET,
"grant_type":"authorization_code",
"code":code,
"redirect_uri":REDIRECT_URI
]
self.request(MethodType.POST, urlString: urlString, parameters: parameters, success: { (response) in
success(response: response)
}) { (error) in
failure(error: error)
}
}
}
OC下:
#import "NetworkTools.h"
/// 网络请求枚举类型
typedef enum : NSUInteger {
GET,
POST,
} CZRequestMethod;
@implementation NetworkTools
+ (instancetype)sharedTools {
static NetworkTools *tools;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
tools = [[self alloc] initWithBaseURL:nil];
// 设置响应数据格式
tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
});
//tools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html" ,nil];
return tools;
}
- (void)request:(CZRequestMethod)method URLString:(NSString *)URLString parameters:(id)parameters finished:(void (^)(id result, NSError *error))finished {
// GET请求
if (method == GET) {
[self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
finished(responseObject, nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
finished(nil, error);
}];
}else{
// 反之 POST请求
[self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
finished(responseObject, nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
finished(nil, error);
}];
}
}
// 使用(.h中提供对外的方法)
- (void)uploadPersonInfo:(NSString *)name age:(NSInteger)age finished:(void (^)(id result, NSError *error))finished{
NSDictionary *paramters = @{@"name":name,@"age":@(age)};
[self request:POST URLString:serverHost parameters:paramters finished:finished];
}
@end