-
Notifications
You must be signed in to change notification settings - Fork 0
/
KRNetworkManager.m
88 lines (73 loc) · 2.35 KB
/
KRNetworkManager.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
//
// KRNetworkManager.m
//
// Created by Kishyr Ramdial on 2012/03/28.
// Copyright (c) 2012 Kishyr Ramdial. All rights reserved.
//
#import "KRNetworkManager.h"
#import "Reachability.h"
@implementation KRNetworkManager
+ (KRNetworkManager *)sharedManager {
static dispatch_once_t once;
static KRNetworkManager *networkManager;
dispatch_once(&once, ^{
networkManager = [[KRNetworkManager alloc] init];
});
return networkManager;
}
- (id)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
}
return self;
}
- (void)startNetworkMonitor {
self.hostReach = [Reachability reachabilityForInternetConnection];
[self.hostReach startNotifier];
[self updateReachability:self.hostReach];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
#pragma mark - Reachability
- (void)reachabilityChanged:(NSNotification *)note {
Reachability *curReach = [note object];
[self updateReachability:curReach];
}
- (void)updateReachability:(Reachability *)curReach {
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus) {
case NotReachable: {
self.connectionAvailable = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"REACH_OFFLINE" object:nil userInfo:nil];
break;
}
case ReachableViaWWAN: {
self.connectionAvailable = YES;
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"WWAN" forKey:@"type"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"REACH_ONLINE" object:nil userInfo:dict];
break;
}
case ReachableViaWiFi: {
self.connectionAvailable = YES;
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"WiFi" forKey:@"type"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"REACH_ONLINE" object:nil userInfo:dict];
break;
}
}
}
- (void)setConnectionAvailable:(BOOL)connectionAvailable {
_connectionAvailable = connectionAvailable;
if (_connectionAvailable && self.connectionIsOnline) {
NSLog(@"1 ONLINE");
self.connectionIsOnline();
}
else {
NSLog(@"1 OFFLINE");
if (self.connectionIsOffline) {
self.connectionIsOffline();
}
}
}
@end