forked from cheesemaker/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUUReachability.m
More file actions
263 lines (209 loc) · 7.7 KB
/
UUReachability.m
File metadata and controls
263 lines (209 loc) · 7.7 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
//
// UUReachability.m
// Useful Utilities - Simple block based reachibilty wrapper
//
// Created by Ryan DeVore on 7/1/14.
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@threejacks.com
//
#import "UUReachability.h"
#define UUIsBitSet(a,b) ((a & b) == b)
NSString * const kUUReachabilityChangedNotification = @"UUReachabilityChangedNotification";
@interface UUReachabilityResult ()
+ (instancetype) reachabilityResultWithFlags:(SCNetworkReachabilityFlags)reachabilityFlags;
@end
@interface UUReachability ()
@property (nonatomic, strong) NSTimer* reachabilityChangedTimer;
- (void) kickReachabilityChanged:(UUReachabilityResult*)result;
@end
static void UUReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
NSCAssert([(__bridge NSObject*) info isKindOfClass: [UUReachability class]], @"info was wrong class in UUReachabilityCallback");
UUReachability* reachability = (__bridge UUReachability*)info;
UUReachabilityResult* result = [UUReachabilityResult reachabilityResultWithFlags:flags];
reachability.currentReachability = result;
[reachability kickReachabilityChanged:result];
}
@implementation UUReachability
{
SCNetworkReachabilityRef _reachabilityRef;
}
+ (instancetype) sharedInstance
{
static id theSharedObject = nil;
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^
{
theSharedObject = [self reachabilityForHostName:@"www.apple.com"];
});
return theSharedObject;
}
+ (instancetype) reachabilityForHostName:(NSString*)hostName
{
UUReachability* obj = [[[self class] alloc] initWithHostName:hostName];
return obj;
}
- (id) initWithHostName:(NSString*)hostName
{
self = [super init];
if (self)
{
self.reachabilityChangedDelay = 1.0f;
_reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
[self startNotifier];
}
return self;
}
- (void) dealloc
{
[self stopNotifier];
if (_reachabilityRef)
{
CFRelease(_reachabilityRef);
}
}
- (void) checkReachability:(void (^)(UUReachabilityResult* result))completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
UUReachabilityResult* result = [self syncrhonousCheckReachability];
if (completion)
{
dispatch_async(dispatch_get_main_queue(), ^
{
completion(result);
});
}
});
}
- (UUReachabilityResult*) syncrhonousCheckReachability
{
UUReachabilityResult* result = nil;
SCNetworkReachabilityFlags flags = 0;
if (_reachabilityRef)
{
if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
{
result = [UUReachabilityResult reachabilityResultWithFlags:flags];
}
}
self.currentReachability = result;
[self kickReachabilityChanged:result];
return result;
}
- (BOOL) startNotifier
{
BOOL returnValue = NO;
SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
if (SCNetworkReachabilitySetCallback(_reachabilityRef, UUReachabilityCallback, &context))
{
if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
{
returnValue = YES;
}
}
return returnValue;
}
- (void) stopNotifier
{
if (_reachabilityRef != NULL)
{
SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}
}
- (void) kickReachabilityChanged:(UUReachabilityResult*)result
{
[self.reachabilityChangedTimer invalidate];
self.reachabilityChangedTimer = [NSTimer scheduledTimerWithTimeInterval:self.reachabilityChangedDelay target:self selector:@selector(delayHandleReachabilityChanged:) userInfo:result repeats:NO];
}
- (void) delayHandleReachabilityChanged:(NSTimer*)timer
{
UUReachabilityResult* result = timer.userInfo;
dispatch_async(dispatch_get_main_queue(), ^
{
[[NSNotificationCenter defaultCenter] postNotificationName:kUUReachabilityChangedNotification object:result];
});
}
- (BOOL) isReachable
{
if (!self.currentReachability)
{
return YES;
}
return [self.currentReachability isReachable];
}
@end
@implementation UUReachabilityResult
+ (instancetype) reachabilityResultWithFlags:(SCNetworkReachabilityFlags)reachabilityFlags
{
UUReachabilityResult* result = [UUReachabilityResult new];
[result updateReachability:reachabilityFlags];
return result;
}
+ (NSDictionary*) reachabilityFlagsToDictionary:(SCNetworkReachabilityFlags)flags
{
NSMutableDictionary* md = [NSMutableDictionary dictionary];
[md setValue:@(flags) forKey:@"RawFlags"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsTransientConnection)) forKey:@"TransientConnection"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsReachable)) forKey:@"Reachable"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsConnectionRequired)) forKey:@"ConnectionRequired"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsConnectionOnTraffic)) forKey:@"ConnectionOnTraffic"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsInterventionRequired)) forKey:@"InterventionRequired"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsConnectionOnDemand)) forKey:@"ConnectionOnDemand"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsIsLocalAddress)) forKey:@"IsLocalAddress"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsIsDirect)) forKey:@"IsDirect"];
[md setValue:@(UUIsBitSet(flags, kSCNetworkReachabilityFlagsIsWWAN)) forKey:@"IsWWAN"];
return md.copy;
}
- (NSDictionary*) reachabilityFlagsAsDictionary
{
return [[self class] reachabilityFlagsToDictionary:self.reachabilityFlags];
}
- (void) updateReachability:(SCNetworkReachabilityFlags)reachabilityFlags
{
self.reachabilityFlags = reachabilityFlags;
self.isReachableWithWiFi = NO;
self.isReachableWithCell = NO;
self.isReachable = NO;
// This logic taken directly from the Apple samples
if ((reachabilityFlags & kSCNetworkReachabilityFlagsReachable) == 0)
{
// The target host is not reachable.
return;
}
if ((reachabilityFlags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
{
//
// If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
//
self.isReachableWithWiFi = YES;
}
if ((((reachabilityFlags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(reachabilityFlags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
//
// ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
//
if ((reachabilityFlags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
//
// ... and no [user] intervention is needed...
//
self.isReachableWithWiFi = YES;
}
}
if ((reachabilityFlags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
//
// ... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
//
self.isReachableWithCell = YES;
}
self.isReachable = (self.isReachableWithWiFi || self.isReachableWithCell);
}
@end