How to detect change in network with Reachability?
Asked Answered
B

3

8

I'm currently checking network connection on viewDidLoad using this:

-(BOOL)reachable {
    ReachabilityDRC *r = [ReachabilityDRC reachabilityWithHostName:@"google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    if(internetStatus == NotReachable) {
        return NO;
    }
    return YES;
}

But I also want to be notified if there is a change of network, such as wifi dropped, or wifi is back, so I can make changes accordingly.

How can I adjust my method to do something like that?

Byte answered 11/11, 2011 at 19:30 Comment(1)
Possible duplicate of: #785082Plonk
K
6

Another possible solution is to add a NS Notification in "application didfinishlaunching":

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability) name:kReachabilityChangedNotification object:nil];

and in checkForReachability method do this:

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
        //Do something
    }
     else if (remoteHostStatus == ReachableViaWiFi) {
    // Do something
 }
    else{

// Else do something else
}
Koroseal answered 11/11, 2011 at 21:28 Comment(1)
This won't work as intended. Reachability initialisation and startNotifier should go in appDidFinishLaunching, because checkForReachability won't be called if you don't startNotifier.Intimist
C
9

1- add SystemConfiguration.framework to your project.

2- Download following files from GitHub

Reachability.h
Reachability.m

3- Add these files in your projects

4- add @class Reachability; in YourViewController.h

#import <UIKit/UIKit.h>

@class Reachability;

5- add variable Reachability* internetReachable; in YourViewController.h

#import <UIKit/UIKit.h>

@class Reachability;

@interface YourViewController : UIViewController {
    Reachability* internetReachable;
}

6- add Reachability.h in YourViewController.m

#import "YourViewController.h"
#import "Reachability.h"

7- add following lines in -(void)ViewDidLoad in YourViewController.m

-(void)ViewDidLoad {
    [[NSNotificationCenter defaultCenter] 
                       addObserver:self 
                       selector:@selector(checkNetworkStatus:) 
                       name:kReachabilityChangedNotification 
                       object:nil];

    internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];
}

8- add following function after -(void)viewDidLoad

-(void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            break;
        }
    }
}

Now every change of internet connection you will see log in console.

Clowers answered 26/8, 2014 at 5:31 Comment(0)
K
6

Another possible solution is to add a NS Notification in "application didfinishlaunching":

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability) name:kReachabilityChangedNotification object:nil];

and in checkForReachability method do this:

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
        //Do something
    }
     else if (remoteHostStatus == ReachableViaWiFi) {
    // Do something
 }
    else{

// Else do something else
}
Koroseal answered 11/11, 2011 at 21:28 Comment(1)
This won't work as intended. Reachability initialisation and startNotifier should go in appDidFinishLaunching, because checkForReachability won't be called if you don't startNotifier.Intimist
F
0

I've used the excellent extension to the Reachability class that the Donoho Design Group has put together. It has notifications that allow you to be alerted when the network status changes.

http://blog.ddg.com/?p=24

Good luck

Finzer answered 11/11, 2011 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.