-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkMonitor.m
73 lines (62 loc) · 2.12 KB
/
NetworkMonitor.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
//
// NetworkMonitor.m
// ServerStatus
//
// Created by Florian Mutter on 30.11.10.
// Copyright 2010 skweez.net. All rights reserved.
//
#import "NetworkMonitor.h"
#import "SynthesizeSingleton.h"
#import <SystemConfiguration/SystemConfiguration.h>
@implementation NetworkMonitor
SYNTHESIZE_SINGLETON_FOR_CLASS(NetworkMonitor)
#pragma mark -
#pragma mark Network Status
- (void)sendNotificationNetworkChanged {
/* Create the data that is send with the notification */
NSDictionary *d = [NSDictionary
dictionaryWithObject:[NSNumber numberWithBool:networkAvailable]
forKey:@"networkAvailable"];
/* Post the notification */
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:NetworkChangeNotification
object:self
userInfo:d];
}
- (void)networkStatusChanged:(SCNetworkReachabilityRef)network withFlags:(SCNetworkConnectionFlags)flags {
/**
* If the network is capable of reaching skweez.net and this is not an
* inactive dialup connection, we are on
**/
if (flags & kSCNetworkFlagsReachable && !(flags & kSCNetworkFlagsConnectionRequired)) {
if (!networkAvailable) {
DLog(@"A network connection is available");
networkAvailable = YES;
[self sendNotificationNetworkChanged];
}
} else {
if (networkAvailable) {
DLog(@"No network connection is available");
networkAvailable = NO;
[self sendNotificationNetworkChanged];
}
}
}
static void networkStatusCallback(SCNetworkReachabilityRef network,
SCNetworkConnectionFlags flags,
void * info
)
{
NetworkMonitor *_self = (NetworkMonitor *)info;
[_self networkStatusChanged:network withFlags:flags];
}
/* We want to be notified if the network status of the machine is changing */
- (void)monitorNetwork {
SCNetworkReachabilityRef network = SCNetworkReachabilityCreateWithName(NULL, "skweez.net");
SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL};
SCNetworkReachabilitySetCallback(network, networkStatusCallback, &context);
SCNetworkReachabilityScheduleWithRunLoop(network,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
}
@end