Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS/iPhone Reachability - How to only check when internet is lost/not reachable using Reachability.m/.h

Currently i am using the class by apple reachability.m/.h and it works, except it notifies me for any change, where as i would like to only notify the user if the network is not reachable. Currently if i have a internet connection and then loose the network it tells me. However when you reconnect to the network it also tells me, which i do not want. I want it to only tell me when there is a loss/no network.

I believe it has something to do with the call:

- (void)viewWillAppear:(BOOL)animated
{
    // check for internet connection
    [[NSNotificationCenter defaultCenter]
          addObserver:self
             selector:@selector(checkNetworkStatus:)
                 name:kReachabilityChangedNotification
               object:nil];

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

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName:
                     @"www.google.ca"] retain];
    [hostReachable startNotifier];

    // now patiently wait for the notification
}

when calling -[NSNotificationCenter addObserver:selector:name:object:], does the name have any other function then being literally a name? this is my first time using NSNotificationCenter so i am not well versed in this matter.

EDIT:

Here is my checkNetworkStatus function: (The problem is i am getting "NotReachable" as the network connection is coming back and NSAlert goes off multiple times)

- (void) checkNetworkStatus:(NSNotification *)notice
{
        // called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)

{
    case NotReachable:
    {
        UIAlertView * alert  = [[UIAlertView alloc] initWithTitle:@"Network Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
        [alert show];
        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;

    }
}

NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)

{
    case NotReachable:
    {
        NSLog(@"A gateway to the host server is down.");

        break;

    }
    case ReachableViaWiFi:
    {
        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;

    }
}

}

like image 825
Mausimo Avatar asked Jan 23 '11 03:01

Mausimo


People also ask

How to turn on one hand mode on iPhone 13?

Enabling Reachability On iPhone 13 Pro Max Once enabled on an iPhone without a Home button, such as the iPhone 13 Pro Max, the user will need to swipe down on the bottom edge of the display to activate the one-handed mode. On an iPhone with a Home button, double-tapping the button will activate one-handed mode.

How to use iPhone with only one hand?

With Android 12 or higher, you can turn on a one-handed mode that lowers the top half of the screen, similar to the Reachability feature on an iPhone. To enable this on most Android phones, go to Settings > System > Gestures > One-handed Mode and turn on the switch for Use one-handed mode.

What is reachability alert?

Notification On Reachability. Through this service you will be notified if you try to call any number and was unreachable or switched off by receiving an SMS indicates that the number become available now.

Is there one hand mode in iPhone?

When you use iPhone with one hand in Portrait orientation, you can use Reachability to lower the top half of the screen so it's within easy reach of your thumb. (Not supported on iPhone SE (1st generation).) Go to Settings > Accessibility > Touch, then turn on Reachability.


2 Answers

Reachability will send a notification when the status has changed, but what you do with that notification is entirely up to you. If you don't want to tell the user that the network is back, you don't have to.

The "name" parameter in the NSNotificationCenter method indicates what notification you are subscribing to. When an object posts a notification, it does so with a particular name.

like image 114
Brian Avatar answered Nov 15 '22 22:11

Brian


If you replace the www.hostname.com with just an IP address, it will only alert once instead of multiple times.

like image 39
ddavtian Avatar answered Nov 15 '22 22:11

ddavtian