0

I have the following code:

- (void) testInternetConnection {
internetConnection = [Reachability reachabilityWithHostname:@"www.google.com"];

// Internet is reachable
internetConnection.reachableBlock = ^(Reachability*reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Yayyy, we have the interwebs!");
    });
};

// Internet is not reachable
internetConnection.unreachableBlock = ^(Reachability*reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Someone broke the internet :(");
    });
};

[internetConnection startNotifier];
}

How do I tell if my internet has changed using the notifier? I understand the singleton method and use that when needed.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
cdub
  • 24,555
  • 57
  • 174
  • 303
  • yes i am using that answer, but how do I check once the notifier is started? – cdub Dec 16 '13 at 04:33
  • using kReachabilityChangedNotification – Jay Gajjar Dec 16 '13 at 04:35
  • and this is always true even when I turn the network off: BOOL status = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable); – cdub Dec 16 '13 at 04:35
  • 1
    ensure BOOL status = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable) , on device if you are testing in simulator – maddy Dec 16 '13 at 04:49
  • +lots to Alok, i didn't realize the simulator connectivity dealt witht hte device too – cdub Dec 16 '13 at 04:53

3 Answers3

2

try to test your code on device when ever possible.

ensure

BOOL status = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable)

, on device if you are testing in simulator

The simulator just uses your default Mac network connection so you need to disconnect your mac from the network and the simulator will experience the same loss of network connectivity.

Thanks

maddy
  • 4,001
  • 8
  • 42
  • 65
0

you can check reachability by this code,

//to make reachability object

reachability = [Reachability reachabilityForInternetConnection];  

//start notifier

[reachability startNotifier];  

//get reachability status

remoteHostStatus = [reachability currentReachabilityStatus];    

Thanks.

Jitendra
  • 5,055
  • 2
  • 22
  • 42
Banker Mittal
  • 1,918
  • 14
  • 26
0
 //First import Reachability classes
// In Appdelegate .h file create variables
  Reachability *hostReach,*internetReach,*wifiReach;
  Reachability *internetReachable;

// After that add this code didfinish lonching with options
 internetReachable = [Reachability reachabilityForInternetConnection] ;
 [internetReachable startNotifier];

 -(BOOL) connectedToNetwork
 {
const char *host_name = "www.google.com";
BOOL _isDataSourceAvailable = NO;
Boolean success;
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
_isDataSourceAvailable = success &&
(flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);

CFRelease(reachability);

return _isDataSourceAvailable;
 }
mahesh chowdary
  • 432
  • 5
  • 13