John
John

Reputation: 7

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.

Upvotes: 0

Views: 291

Answers (3)

John
John

Reputation: 7

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");
    });
};

Upvotes: 0

dana0550
dana0550

Reputation: 1125

To check for internet connection you can use this:

- (BOOL)connected {
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}

This returns a BOOL which you can use in a conditional to check for an internet connection.

NOTE: Be Sure to add the the proper frameworks and add the following at the top of your .m file:

#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>

UPDATE #1:

Based on your comment it seems that you want to test for connectivity via a timer. The following code will check for internet every second. This is not recommended but this should accomplish what you are asking:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/60.0)
                                                  target:self
                                                selector:@selector(checkInterWeb)
                                                userInfo:nil
                                                 repeats:YES];

Also you should create an ivar to the timer so you can invalidate it when you no longer need it like so:

if(timer) {
    [timer invalidate];
    timer = nil;
}

Upvotes: 0

dar512
dar512

Reputation: 872

how can I check interner connection before my app load

You can check the internet connection in appdelegate. You just can't do anything to inform the user about it. You need the window (and I believe the first view) before you can put up an alert.

What are you trying to achieve?

Upvotes: 1

Related Questions