Search code examples
objective-creachability

check internet connectionreachability


I check internet connection with Reachability in my App. How can I check user connectivity contentiously, if his connection fails or change from wi-fi to 3G how can I be informed ?

right now here is my code:

- (void)viewDidLoad
{
[super viewDidLoad];

Reachability *myNetwork = [Reachability reachabilityWithHostname:@"google.com"];
NetworkStatus myStatus = [myNetwork currentReachabilityStatus];

switch (myStatus) {
    case NotReachable:
    { NSLog(@"There's no internet connection at all.");
        [self performSegueWithIdentifier: @"noInternet" sender: self];
    }
        break;

    case ReachableViaWWAN:
        NSLog(@"We have a 3G connection");
        break;

    case ReachableViaWiFi:
        NSLog(@"We have WiFi.");
        break;

    default:
        break;
}

how can I check interner connection before my app load, I tried this code in Appdelegate.m but I got an error because of performSegueWithIdentifier methos.


Solution

  • I found the answer for checking any changes in user internet connection:

    Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];
    
    reach.reachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
           NSLog(@"Block Says Reachable") ;
        });
    };
    
    reach.unreachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
           NSLog(@"Block Says Unreachable");
        });
    };