-
Notifications
You must be signed in to change notification settings - Fork 0
/
WBSNetworking.m
90 lines (66 loc) · 2.89 KB
/
WBSNetworking.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// WBSNetworking.m
// WBSBlog
//
// Created by Weberson on 2017/2/9.
// Copyright © 2017年 Weberson. All rights reserved.
//
#import "WBSNetworking.h"
@implementation WBSClient
+ (instancetype)sharedClient {
static WBSClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[WBSClient alloc] initWithBaseURL:nil];
//告诉AFN,下载下来的数据是JSON,直接解析返回给我们
_sharedClient.responseSerializer = [AFJSONResponseSerializer serializer];
_sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", nil];
});
return _sharedClient;
}
@end
@implementation WBSNetworking
// 常用 get
+ (NSURLSessionDataTask *)GETRequest:(NSString *)URLString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLSessionDataTask * sessionDataTask = [[WBSClient sharedClient] GET:URLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
// 进度
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
@try {
success(responseObject);
}
@catch (NSException *exception) {
}
@finally {
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
failure(error);
}];
return sessionDataTask;
}
// 常用 post
+ (NSURLSessionDataTask *)POSTRequest:(NSString *)URLString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLSessionDataTask *sessionDataTask = [[WBSClient sharedClient] POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
// 进度
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 成功
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
@try {
// 将成功请求请求的数据 返回
success(responseObject);
}
@catch (NSException *exception) {
}
@finally {
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 失败
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
failure(error);
}];
return sessionDataTask;
}
@end