-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
299 lines (228 loc) · 10.4 KB
/
ViewController.m
File metadata and controls
299 lines (228 loc) · 10.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// ViewController.m
// CCNetworkManager
//
// Created by WeiPeng on 16/8/12.
// Copyright © 2016 WeiPeng. All rights reserved.
//
#import "ViewController.h"
#import "CCNetworkManager.h"
#import "CCHTTPRequest.h"
#import "AFNetworking.h"
#ifdef DEBUG
#define CCLog(...) printf("[%s] %s [第%d行]: %s\n", __TIME__ ,__PRETTY_FUNCTION__ ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define CCLog(...)
#endif
#define CCMaxYOfView(view) CGRectGetMaxY(view.frame)
#define CCMaxXOfView(view) CGRectGetMaxX(view.frame)
#define CCMinYOfView(view) CGRectGetMinY(view.frame)
#define CCMinXOfView(view) CGRectGetMinX(view.frame)
static NSString *const dataUrl = @"http://api.budejie.com/api/api_open.php";
static NSString *const downloadUrl = @"http://wvideo.spriteapp.cn/video/2016/0328/56f8ec01d9bfe_wpd.mp4";
@interface ViewController ()
@property (weak, nonatomic) UITextView *networkDataView;
@property (weak, nonatomic) UITextView *cacheDataView;
@property (weak, nonatomic) UILabel *cacheStatusLabel;
@property (weak, nonatomic) UISwitch *cacheSwitch;
@property (weak, nonatomic) UIProgressView *progressView;
@property (weak, nonatomic) UIButton *downloadButton;
/** 是否开启缓存*/
@property (nonatomic, assign, getter=isCache) BOOL cache;
/** 是否开始下载*/
@property (nonatomic, assign, getter=isDownload) BOOL download;
@end
@implementation ViewController
- (void)setupSubviews{
self.view.backgroundColor = [UIColor whiteColor];
UITextView *networkDataView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 40, 100)];
networkDataView.layer.borderWidth = 1.f;
self.networkDataView = networkDataView;
[self.view addSubview:networkDataView];
UITextView *cacheDataView = [[UITextView alloc] initWithFrame:CGRectMake(20, CCMaxYOfView(networkDataView) + 20, [UIScreen mainScreen].bounds.size.width - 40, 100)];
cacheDataView.layer.borderWidth = 1.f;
self.cacheDataView = cacheDataView;
[self.view addSubview:cacheDataView];
UILabel *cacheStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CCMaxYOfView(cacheDataView) + 20, 100, 44)];
cacheStatusLabel.layer.borderWidth = 1.f;
self.cacheStatusLabel = cacheStatusLabel;
[self.view addSubview:cacheStatusLabel];
UISwitch *cacheSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(CCMaxXOfView(cacheStatusLabel) + 20, CCMinYOfView(cacheStatusLabel), 100, 44)];
self.cacheSwitch = cacheSwitch;
[self.view addSubview:cacheSwitch];
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20, CCMaxYOfView(cacheSwitch) + 30, [UIScreen mainScreen].bounds.size.width - 40, 20)];
self.progressView = progressView;
[self.view addSubview:progressView];
UIButton *downloadButton = [[UIButton alloc] initWithFrame:CGRectMake(100, CCMaxYOfView(progressView) + 20, 100, 44)];
[downloadButton addTarget:self action:@selector(downloadButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
downloadButton.backgroundColor = [UIColor blueColor];
[downloadButton setTitle:@"Download" forState:UIControlStateNormal];
self.downloadButton = downloadButton;
[self.view addSubview:downloadButton];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//setup subviews
[self setupSubviews];
/**
设置网络请求参数的格式:默认为二进制格式
PPRequestSerializerJSON(JSON格式),
PPRequestSerializerHTTP(二进制格式)
设置方式 : [PPNetworkHelper setRequestSerializer:PPRequestSerializerHTTP];
*/
/**
设置请求头 : [PPNetworkHelper setValue:@"value" forHTTPHeaderField:@"header"];
*/
// 开启日志打印
[CCNetworkManager openLog];
// 获取网络缓存大小
double cacheDataSize = [CCNetworkCache getAllHttpCacheSize]/1024.f;
CCLog(@"网络缓存大小cache = %fKB",cacheDataSize);
// 清理缓存 [PPNetworkCache removeAllHttpCache];
// 实时监测网络状态
[self monitorNetworkStatus];
/*
* 获取当前网络状态
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self getCurrentNetworkStatus];
});
[self CCHTTPRequestLayerDemo];
}
- (void)CCHTTPRequestLayerDemo
{
// 登陆
[CCHTTPRequest getLoginWithParameters:@"参数" success:^(id response) {
} failure:^(NSError *error) {
}];
// 退出
[CCHTTPRequest getExitWithParameters:@"参数" success:^(id response) {
} failure:^(NSError *error) {
}];
}
#pragma mark - 获取数据请求示例 GET请求自动缓存与无缓存
- (void)getData:(BOOL)isOn url:(NSString *)url
{
NSDictionary *para = @{ @"a":@"list", @"c":@"data",@"client":@"iphone",@"page":@"0",@"per":@"10", @"type":@"29"};
// 自动缓存
if(isOn)
{
self.cacheStatusLabel.text = @"缓存打开";
self.cacheSwitch.on = YES;
[CCNetworkManager GET:url parameters:para responseCache:^(id responseCache) {
// 1.先加载缓存数据
self.cacheDataView.text = [self jsonToString:responseCache];
} success:^(id responseObject) {
// 2.再请求网络数据
self.networkDataView.text = [self jsonToString:responseObject];
} failure:^(NSError *error) {
}];
}
// 无缓存
else
{
self.cacheStatusLabel.text = @"缓存关闭";
self.cacheSwitch.on = NO;
self.cacheDataView.text = @"";
[CCNetworkManager GET:url parameters:para success:^(id responseObject) {
self.networkDataView.text = [self jsonToString:responseObject];
} failure:^(NSError *error) {
}];
}
}
#pragma mark - 实时监测网络状态
- (void)monitorNetworkStatus
{
// 网络状态改变一次, networkStatusWithBlock就会响应一次
[CCNetworkManager networkStatusWithBlock:^(CCNetworkStatusType networkStatus) {
switch (networkStatus) {
// 未知网络
case CCNetworkStatusUnknown:
// 无网络
case CCNetworkStatusNotReachable:
self.networkDataView.text = @"没有网络";
[self getData:YES url:dataUrl];
CCLog(@"无网络,加载缓存数据");
break;
// 手机网络
case CCNetworkStatusReachableViaWWAN:
// 无线网络
case CCNetworkStatusReachableViaWiFi:
[self getData:[[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"] url:dataUrl];
CCLog(@"有网络,请求网络数据");
break;
}
}];
}
#pragma mark - 获取当前最新网络状态
- (void)getCurrentNetworkStatus
{
if (kIsNetwork) {
CCLog(@"有网络");
if (kIsWWANNetwork) {
CCLog(@"手机网络");
}else if (kIsWiFiNetwork){
CCLog(@"WiFi网络");
}
} else {
CCLog(@"无网络");
}
}
#pragma mark - 下载
- (void)downloadButtonDidClicked:(UIButton *)sender{
static NSURLSessionTask *task = nil;
if (!self.isDownload) {
self.download = YES;
[self.downloadButton setTitle:@"Cancel Download" forState:UIControlStateNormal];
task = [CCNetworkManager downloadWithURL:downloadUrl fileDir:@"Download" progress:^(NSProgress *progress) {
CGFloat status = 100.f * progress.completedUnitCount / progress.totalUnitCount;
self.progressView.progress = status / 100.f;
CCLog(@"Downloading: %.2f%%,,%@", status, [NSThread currentThread]);
} success:^(NSString *filePath) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Download completed!"
message:[NSString stringWithFormat:@"file path:%@", filePath] preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:^{
}];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([action.title containsString:@"OK"]) {
[self dismissViewControllerAnimated:alertController completion:^{
}];
}
}];
[alertController addAction:confirmAction];
[self.downloadButton setTitle:@"Download" forState:UIControlStateNormal];
CCLog(@"filepath = %@", filePath);
} failure:^(NSError *error) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Download Failed!"
message:[NSString stringWithFormat:@"ERROR:%@", error] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([action.title containsString:@"OK"]) {
[self dismissViewControllerAnimated:alertController completion:^{
}];
}
}];
[alertController addAction:confirmAction];
[self presentViewController:alertController animated:YES completion:^{
}];
CCLog(@"ERROR:%@", error);
}];
} else {
self.download = NO;
[task suspend];
self.progressView.progress = 0;
[self.downloadButton setTitle:@"Download" forState:UIControlStateNormal];
}
}
/**
* json转字符串
*/
- (NSString *)jsonToString:(NSDictionary *)dic
{
if(!dic){
return nil;
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
@end