Last active
December 13, 2015 23:49
-
-
Save andrealufino/4994901 to your computer and use it in GitHub Desktop.
This is a singleton class to detect internet connection. It can be customized to check if internet connection is over WiFi or cellular.
Example of use : if ([ALData checkInternetConnection]) NSLog(@"Internet available");
else NSLog(@"Internet unavailable");
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
// | |
// ALData.h | |
// AL | |
// | |
// Created by Andrea Lufino on 09/05/12. | |
// Copyright 2012 Andrea Lufino. All rights reserved. | |
#import <Foundation/Foundation.h> | |
#import <SystemConfiguration/SystemConfiguration.h> | |
#import <sys/socket.h> | |
#import <netinet/in.h> | |
#import "ALMacros.h" | |
/*! | |
A singleton class useful to check the internet connection. Needs the SystemConfiguration framework. | |
*/ | |
@interface ALData : NSObject | |
/*! | |
---------------------------- | |
/// @name Internet check | |
----------------------------- | |
*/ | |
/*! | |
This method check the internet connection. | |
@return A BOOL value based on the presence or less of the internet connection. YES if internet connection is available, NO if it isn't | |
*/ | |
+ (BOOL)checkInternetConnection; | |
@end |
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
// | |
// ALData.m | |
// AL | |
// | |
// Created by Andrea Lufino on 09/05/12. | |
// Copyright 2012 Andrea Lufino. All rights reserved. | |
#import "ALData.h" | |
@implementation ALData | |
#pragma mark - Singleton methods | |
+ (ALData *)shared { | |
static ALData *_shared; | |
if(!_shared) { | |
static dispatch_once_t oncePredicate; | |
dispatch_once(&oncePredicate, ^ { | |
_shared = [[super allocWithZone:nil] init]; | |
}); | |
} | |
return _shared; | |
} | |
+ (id)allocWithZone:(NSZone *)zone { return [self shared]; } | |
- (id)copyWithZone:(NSZone *)zone { return self; } | |
#if (!__has_feature(objc_arc)) | |
- (id)retain { return self; } | |
- (unsigned)retainCount { return UINT_MAX; } | |
- (void)release {} | |
- (id)autorelease { return self; } | |
#endif | |
#pragma mark - Custom methods | |
+ (BOOL)checkInternetConnection { | |
SINGLETON | |
struct sockaddr_in zeroAddress; | |
bzero(&zeroAddress, sizeof(zeroAddress)); | |
zeroAddress.sin_len = sizeof(zeroAddress); | |
zeroAddress.sin_family = AF_INET; | |
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress); | |
if(reachability != NULL) { | |
//NetworkStatus retVal = NotReachable; | |
SCNetworkReachabilityFlags flags; | |
if (SCNetworkReachabilityGetFlags(reachability, &flags)) { | |
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) | |
{ | |
// if target host is not reachable | |
return NO; | |
} | |
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) | |
{ | |
// if target host is reachable and no connection is required | |
// then we'll assume (for now) that your on Wi-Fi | |
return YES; | |
} | |
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || | |
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) | |
{ | |
// ... and the connection is on-demand (or on-traffic) if the | |
// calling application is using the CFSocketStream or higher APIs | |
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) | |
{ | |
// ... and no [user] intervention is needed | |
return YES; | |
} | |
} | |
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) | |
{ | |
// ... but WWAN connections are OK if the calling application | |
// is using the CFNetwork (CFSocketStream?) APIs. | |
return YES; | |
} | |
} | |
} | |
return NO; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment