0

I'm trying to use reachability in Xcode 4.4 to alert the user if he/she is not connected to the internet. My initial view controller has a button which loads a table (which is populated from a plist online). I followed a few examples in Stack Overflow but could not get it working. Here's a snippet of my .h file:

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

@interface MainPageViewController : UIViewController
{
  Reachability *internetReachable;
  Reachability *hostReachable;
}

-(void) checkNetworkStatus: (NSNotification *)notice;
@property BOOL internetActive;
@property BOOL hostActive;

@end

Here's my .m file:

#import "MainPageViewController.h"
#import "Reachability.h"

@implementation MainPageViewController
@synthesize internetActive;
@synthesize hostActive;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
  }
 return self;
}

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

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

    hostReachable = [Reachability reachabilityWithHostname: @"sites.google.com"];
    [hostReachable startNotifier];
  }

- (void) checkNetworkStatus:(NSNotification *)notice
  {
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];

    if((internetStatus == NotReachable) && (hostStatus == NotReachable))
    {
       UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle:@"Network Error!"
       message: @"You are not connected to the internet!" delegate: self
       cancelButtonTitle: @"Ok" otherButtonTitles: nil];
       [internetAlert show];
       self.internetActive = NO;
       self.hostActive = NO;
    }
  }

-(void) dealloc
  {
    [[NSNotificationCenter defaultCenter] removeObserver: self];
  }

- (void)viewDidLoad
  {
    [super viewDidLoad];
// Do any additional setup after loading the view.
  }

- (void)viewDidUnload
  {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
  }

- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
   {
   return YES;
   }

@end

Since I'm using a button, should I use an IBAction to check for internet before I navigate to the next page?

bumpfox
  • 17
  • 5

1 Answers1

0

You can check for internet wherever is convenient. Consider checking for it in the viewDidLoad or didFinishLaunching methods, or right before you are going to need an internet connection. It may not be as convenient to have a "Is the internet available?" button without any extra action tied to it.

Here is a function that will return whether you are connected. I typically run this every so often during an app's process (or before every request if I'm paranoid).

- (BOOL) connectedToNetwork
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    BOOL internet;
    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
        internet = NO;
    } else {
        internet = YES;
    }
    return internet;
} 

You can then use it like this (assuming you are referencing it from the current object):

if(![self connectedToNetwork]) {
    //Do whatever you need to if you're not connected
} else {
    // You're connected so let the games begin
}
Benjamin Oman
  • 1,654
  • 1
  • 17
  • 19