Created
July 13, 2015 15:14
-
-
Save renekann/85739217710b30b74721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *const networkStateChanged = @"networkStateChanged"; | |
@implementation AppState | |
SYNTHESIZE_SINGLETON_FOR_CLASS(AppState) | |
- (void)startNetworkCheck { | |
if(!self.reachability) { | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(reachabilityChanged:) | |
name:kReachabilityChangedNotification | |
object:nil]; | |
self.reachability = [Reachability reachabilityWithHostname:@"www.google.com"]; | |
[self.reachability startNotifier]; | |
} | |
} | |
- (void)reachabilityChanged:(NSNotification*)note | |
{ | |
Reachability* r = [note object]; | |
NetworkStatus ns = r.currentReachabilityStatus; | |
BOOL tmpNetworkState = self.isConnected; | |
if (ns == NotReachable) { | |
self.isConnected = NO; | |
DDLogInfo(@"reachabilityChanged NOT connected"); | |
} else { | |
self.isConnected = YES; | |
DDLogInfo(@"reachabilityChanged CONNECTED"); | |
} | |
if(tmpNetworkState != self.isConnected) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[[NSNotificationCenter defaultCenter] postNotificationName:networkStateChanged object:nil]; | |
}); | |
} | |
} | |
- (BOOL)hasNetwork { | |
return self.isConnected; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment