Skip to content

Instantly share code, notes, and snippets.

@renekann
Created July 13, 2015 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renekann/85739217710b30b74721 to your computer and use it in GitHub Desktop.
Save renekann/85739217710b30b74721 to your computer and use it in GitHub Desktop.
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