1

I have implemented Apple's Reachability 2.2 class in a new iOS 4.2 project. I simply want an alert view to appear when the device loses network connectivity. ( And thence comply with the interface usability requirement for the App Store. ) I used this previous SO question as my starting point. The app seems to be notifying correctly ( when connectivity is interrupted or restored) , but I get looping with my NS Alert views. I think my error must be somewhat basic, but I cannot catch it. If there is a cleaner way to do this without NS AlertView I am open to that as well. I left some the methods out of the code below, but the app is pretty straightforward, having only one ViewController.

ViewController.h :

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

@class Reachability;

@interface ViewController : UIViewController   {

IBOutlet UITextView *liveOutputTextView;
IBOutlet UITextView *textView;
Reachability* internetReachable;
    Reachability* hostReachable;

}

-(IBAction)action1:(id)sender;
-(IBAction)action2:(id)sender;

-(void)textFieldDidUpdate:(id)sender;
-(void)checkNetworkStatus:(NSNotification *)notice;

@end

ViewController.m

#import "ViewController.h"
#import "Reachability.h"

@implementation ViewController

@synthesize liveOutputTextField;

- (void)checkNetworkStatus:(NSNotification *)notice

{
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

switch (internetStatus)

case NotReachable:
    {
        NSLog(@"The internet is inaccessible.");


        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Internet inaccessible."
                                                         message:@"Internet inaccessible."
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"  
       otherButtonTitles:nil];

        [alert show];
        [alert release];

        break;

    }
    case ReachableViaWiFi:

    {
        NSLog(@"Internet Connetion is UP.");


        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Internet is Up"
                                                         message:@"Internet Up"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles:nil];
        [alert show];
        [alert release];

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


        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Host is Down"
                                                         message:@"Host Down"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles:nil];
        [alert show];
        [alert release];

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

}



- (void)viewDidLoad {

// 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.apple.com"]retain ];
[hostReachable startNotifier];

// now patiently wait for the notification


}

- (void)viewDidAppear:(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.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification

}


-(IBAction) action1:(id)sender {

// 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.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification

//  ** LEFT OUT ACTUAL CODE FOR BREVITY  ** 

}

-(IBAction) action2:(id)sender {

// 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.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification

//  ** LEFT OUT ACTUAL CODE FOR BREVITY  ** 

}

 - (void)viewDidUnload {

// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;


  [[NSNotificationCenter defaultCenter] removeObserver:self];   
}


- (void)dealloc {

[super dealloc];

}
Community
  • 1
  • 1
mozzer
  • 241
  • 1
  • 6
  • 15

2 Answers2

1

Remember that after a notification has been sent and processed, that notification needs to be removed from the Notification Center. Otherwise whenever something in your reachability status changes you'll get an alert.

For example, for remove a notification, you can do:

[[NSNotificationCenter defaultCenter] removeObserver:self
 name:kReachabilityChangedNotification 
 object:nil];

Remember to add it again later if you need to.

aqua
  • 3,014
  • 24
  • 36
  • Thanks. I have [[NSNotificationCenter defaultCenter] removeObserver:self]; in my -(void)viewDidUnload ... Is it in the wrong location? – mozzer Jan 23 '11 at 23:11
0

Adding notification view did appear is enough to handle response. If you add more times then the call back called for many times.

Gowtham R
  • 215
  • 3
  • 7