-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSNetworkManager.m
171 lines (145 loc) · 6.43 KB
/
LSNetworkManager.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// LSNetworkManager.m
// LSNetworkManager
//
// Created by StephenChen on 15/9/21.
// Copyright (c) 2015年 Lansion. All rights reserved.
//
#import "LSNetworkManager.h"
static LSNetworkManager* mNetwork;
static NSObject<LSNetworkStatus> *mStatusHandler;
@implementation LSNetworkManager
+ (BOOL)setUp{
if (!mNetwork) {
mNetwork = [[LSNetworkManager alloc]init];
mNetwork.httpManager = [AFHTTPRequestOperationManager manager];
mNetwork.httpManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil];
mNetwork.httpManager.requestSerializer = [AFHTTPRequestSerializer serializer];
mNetwork.httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
[self reach];
}
return YES;
}
+ (enum NetworkType)getStatus{
if ([AFNetworkReachabilityManager sharedManager].reachableViaWiFi) {
return Wifi;
}else if([AFNetworkReachabilityManager sharedManager].reachableViaWWAN){
return Mobile;
}else{
return Unavail;
};
}
+ (void)setNetworkStatusHandler:(NSObject<LSNetworkStatus>*) statusHandler{
mStatusHandler = statusHandler;
}
+ (void)send:(LSBaseRequest*)request{
if (!mNetwork) {
return;
}
switch (request.method) {
case GET:
[self Get:request];
break;
case POST:
[self Post:request];
break;
case POST_FILE:
[self PostFile:request];
default:
break;
}
}
+ (void)Get:(LSBaseRequest*)request{
NSString* path = (request.path == nil)? @"": request.path;
NSString* url = [NSString stringWithFormat:@"%@%@", request.baseUrl, path];
[mNetwork.httpManager GET:url parameters:request.params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (request.responser != nil) {
NSError *error = nil;
NSData *responseData = responseObject;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
[request.responser success:responseDic withTag:request.tag];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
if (request.responser != nil) {
[request.responser failure:error withTag:request.tag];
}
}];
}
+ (void)Post:(LSBaseRequest*)request{
NSString* path = (request.path == nil)? @"": request.path;
NSString* url = [NSString stringWithFormat:@"%@%@", request.baseUrl, path];
[mNetwork.httpManager POST:url parameters:request.params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (request.responser != nil) {
NSError *error = nil;
NSData *responseData = responseObject;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
[request.responser success:responseDic withTag:request.tag];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (request.responser != nil) {
[request.responser failure:error withTag:request.tag];
}
}];
}
+ (void)PostFile:(LSBaseRequest*)request{
NSString *path = (request.path == nil)? @"": request.path;
NSString *url = [NSString stringWithFormat:@"%@%@", request.baseUrl, path];
NSString *fileName = @"fileName";
NSMutableURLRequest *mRequest = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:request.params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:request.filePath] name:KEY_FILE fileName:fileName mimeType:request.fileType error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
AFHTTPResponseSerializer *respSerializer = [AFHTTPResponseSerializer serializer];
respSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil];
manager.responseSerializer = respSerializer;
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:mRequest progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
if (request.responser != nil) {
[request.responser failure:error withTag:request.tag];
}
} else {
NSLog(@"%@ %@", response, responseObject);
if (request.responser != nil) {
NSError *error = nil;
NSData *responseData = responseObject;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
[request.responser success:responseDic withTag:request.tag];
}
}
}];
[uploadTask resume];
}
+ (void)cancelRequest{
[[mNetwork.httpManager operationQueue] cancelAllOperations];
}
+ (void)reach{
/**
AFNetworkReachabilityStatusUnknown = -1, // 未知
AFNetworkReachabilityStatusNotReachable = 0, // 无连接
AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G
AFNetworkReachabilityStatusReachableViaWiFi = 2, // wifi
*/
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (mStatusHandler == nil) {
return;
}
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
case AFNetworkReachabilityStatusUnknown:
[mStatusHandler lsNetworkStatus:Unavail];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
[mStatusHandler lsNetworkStatus:Wifi];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
[mStatusHandler lsNetworkStatus:Mobile];
break;
default:
break;
}
}];
}
@end