forked from dustturtle/RealReachability
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
188 lines (153 loc) · 5.19 KB
/
ViewController.m
File metadata and controls
188 lines (153 loc) · 5.19 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
//
// ViewController.m
// RealReachability
//
// Created by Dustturtle on 16/1/9.
// Copyright © 2016 Dustturtle. All rights reserved.
//
#import "ViewController.h"
#import "RealReachability.h"
#import "NSObject+SimpleKVO.h"
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UILabel *flagLabel;
@property (nonatomic, strong) UIAlertView *alert;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(VPNStatusChanged:)
name:kRRVPNStatusChangedNotification
object:nil];
ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
NSLog(@"Initial reachability status:%@",@(status));
[self setupFlagLabelWithStatus:status
isVPNOn:[GLobalRealReachability isVPNOn]
accessType:[GLobalRealReachability currentWWANtype]];
self.alert = [[UIAlertView alloc] initWithTitle:@"RealReachability" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (IBAction)testAction:(id)sender
{
// NSLog(@"begin");
// // performance test of this method; ok.
// for (NSInteger i=0; i<10000; i++)
// {
// [GLobalRealReachability isVPNOn];
// }
// NSLog(@"end");
[GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
switch (status)
{
case RealStatusNotReachable:
{
self.alert.message = @"Nothing to do! offlineMode";
[self.alert show];
break;
}
case RealStatusViaWiFi:
{
self.alert.message = @"Do what you want! free!";
[self.alert show];
break;
}
case RealStatusViaWWAN:
{
self.alert.message = @"Take care of your money! You are in charge!";
break;
}
default:
{
self.alert.message = @"Error status, needs debugging!";
break;
}
}
[self.alert show];
}];
}
- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];
ReachabilityStatus previousStatus = [reachability previousReachabilityStatus];
NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus));
[self setupFlagLabelWithStatus:status
isVPNOn:[GLobalRealReachability isVPNOn]
accessType:[GLobalRealReachability currentWWANtype]];
}
- (void)VPNStatusChanged:(NSNotification *)notification
{
// refreshing the status.
[self setupFlagLabelWithStatus:[GLobalRealReachability currentReachabilityStatus]
isVPNOn:[GLobalRealReachability isVPNOn]
accessType:[GLobalRealReachability currentWWANtype]];
}
- (void)setupFlagLabelWithStatus:(ReachabilityStatus)status
isVPNOn:(BOOL)isVPNOn
accessType:(WWANAccessType)accessType
{
NSMutableString *labelStr = [@"" mutableCopy];
switch (status)
{
case RealStatusNotReachable:
{
[labelStr appendString:@"Network unreachable! "];
break;
}
case RealStatusViaWiFi:
{
[labelStr appendString:@"Network wifi! Free! "];
break;
}
case RealStatusViaWWAN:
{
[labelStr appendString:@"WWAN in charge! "];
break;
}
case RealStatusUnknown:
{
[labelStr appendString:@"Unknown status! Needs debugging! "];
break;
}
default:
{
[labelStr appendString:@"Status error! Needs debugging! "];
break;
}
}
if (isVPNOn)
{
[labelStr appendString:@"VPN On! "];
}
if (status == RealStatusViaWWAN)
{
NSString *descStr;
if (accessType == WWANType2G)
{
descStr = @"2G";
}
else if (accessType == WWANType3G)
{
descStr = @"3G";
}
else if (accessType == WWANType4G)
{
descStr = @"4G";
}
else
{
descStr = @"Unknown Status, might be iOS6";
}
[labelStr appendString:descStr];
}
self.flagLabel.text = [labelStr copy];
}
@end