Downloaded Reachability from Apple, using this method to check for an active connection:
-(BOOL)isReachable{
Reachability *r = [Reachability reachabilityWit
Use this code to check whether the device is connected to internet or not
use this code in viewDidLoad :
Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
[hostReachable startNotifier];
and add this function to your code:
-(void) checkNetworkStatus:(NSNotification *)notice
{
recheabilityBool=FALSE;
nonrecheabilityBool=FALSE;
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
nonrecheabilityBool=TRUE;
internetCon=0;
//NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:@"conKey"];
//NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
internetCon=0;
if( nonrecheabilityBool==FALSE)
{
//NSLog(@"A gateway to the host server is down.");
}
break;
}
case ReachableViaWiFi:
{
if(recheabilityBool==FALSE)
{
recheabilityBool=TRUE;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:@"conKey"];
//NSLog(@"The internet is working via WIFI.");
break;
}
//NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
EDITED: you should remove the protocol, http
from your reachabilityWithHostName
call, so updated it to
Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
Another complete answer:
-(BOOL) isReachable:(NSString *)url
{
//Retrieve the host name by given url address.
NSString *hostName = [[NSURL URLWithString:url] host];
Reachability *r = [Reachability reachabilityWithHostName:hostName];
if(NotReachable == [r currentReachabilityStatus]) {
NSLog(@"Not Reachable");
return NO;
}
NSLog(@"Reachable");
return YES;
}
I use KSReachability. It works with NSNotification, blocks, KVO, and with or without ARC.
KSReachability *reachability = [KSReachability reachabilityToHost:@"www.google.com"];
reachability.onReachabilityChanged = ^(KSReachability* reachability) {
NSLog(@"Update: network is %@", reachability.reachable ? @"up." : @"down.");
};
If you are trying to see if the device can reach the internet in general you should probably use reachabilityForInternetConnection instead of reachabilityWithHostName:. Also, both of these calls will take a little bit of time to start up (it will still be in the milliseconds but longer than the time it takes to reach the if condition on the next line.) Here's an example of a singleton class that uses reachability.
static NetworkManager* sharedInstance = nil;
@interface NetworkManager()
@property (nonatomic, strong) Reachability* reachability;
@end
@implementation NetworkManager
@synthesize reachability;
+ (NetworkManager*)sharedInstance
{
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[NetworkManager alloc] init];
}
}
return sharedInstance;
}
- (id)init
{
reachability = [WYReachability reachabilityForInternetConnection];
}
- (BOOL)isNetworkReachable
{
return ([self.reachability currentReachabilityStatus] != NotReachable);
}
@end
To check for the network reachable in other classes you can use.
[[NetworkManager sharedInstance] isNetworkReachable];
I have a same issue with the Tony Million's Reachability : network status was always set to NotReachable. I fix it with adding the SystemConfiguration Framework
Hope it helps