views:

246

answers:

2

There is so much info out there on using Apple's Reachability example, and so much is conflicting. I'm trying to find out of I'm using it (Reachability 2.0) correctly below. My App use case is this: If an internet connection is available through any means (wifi, LAN, Edge, 3G, etc.) a UIButton ("See More") is visible on various views. If no connection, the button is not visible. The "See More" part is NOT critical in any way to the app, it's just an add-on feature. "See More" could be visible or not anytime during the application lifecycle as connection is established or lost. Here's how I did it - Is this correct and/or is there a better way?

Any help is Greatly Appreciated! lq

//  AppDelegate.h

#import "RootViewController.h"

@class Reachability;

@interface AppDelegate : NSObject <UIApplicationDelegate> 
{
    UIWindow *window;
    UINavigationController *navigationController;
    RootViewController *rootViewController;
    Reachability* hostReach;
    // NOT USED: Reachability* internetReach;
    // NOT USED: Reachability* wifiReach;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;

@end


//  AppDelegate.m

#import "AppDelegate.h"
#import "Reachability.h"

#define kHostName @"www.somewebsite.com"

@implementation AppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize rootViewController;

- (void) updateInterfaceWithReachability: (Reachability*) curReach {

    if(curReach == hostReach) {

        NetworkStatus netStatus = [curReach currentReachabilityStatus];
        BOOL connectionRequired = [curReach connectionRequired];

        // Set a Reachability BOOL value flag in rootViewController 
        // to be referenced when opening various views

        if ((netStatus != ReachableViaWiFi) && (netStatus != ReachableViaWWAN)) {
            rootViewController.bConnection = (BOOL *)0;
        } else {
           rootViewController.bConnection = (BOOL *)1;
        }

    } 
}

- (void) reachabilityChanged: (NSNotification* )note {

     Reachability* curReach = [note object];
     NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
     [self updateInterfaceWithReachability: curReach];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

    // NOTE: #DEFINE in Reachability.h: 
     // #define kReachabilityChangedNotification   @"kNetworkReachabilityChangedNotification"

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

    hostReach = [[Reachability reachabilityWithHostName: kHostName] retain];
    [hostReach startNotifer];
    [self updateInterfaceWithReachability: hostReach];

    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];

}

- (void)dealloc {
    [navigationController release];
    [rootViewController release];
    [window release];
    [super dealloc];
}

@end
A: 

Why not rootViewController.bConnection = (netStatus != NotReachable);, which will still work if they add another type of connectivity?

(BOOL *) looks like a mistake (it's equivalent to (signed char *)). In general, I prefer C99's bool and the C99 specification specifically allows for bool blah:1; if you're worried about space.

Also, be careful with the idea that you can check for an "internet connection" — you can check for a network connection, but your ISP could be down, or the host you want to connect to could be down, or someone could've drilled through an undersea cable. If kHostName is the hostname you care about connecting to, then it'll do the right thing.

tc.
A: 

Thanks for the quick response! So what I'm gathering is all I need to do is to change the above to:

- (void) updateInterfaceWithReachability: (Reachability*) curReach { 

    if(curReach == hostReach) { 

        NetworkStatus netStatus = [curReach currentReachabilityStatus]; 
        BOOL connectionRequired = [curReach connectionRequired]; 

        // Set a Reachability BOOL value flag in rootViewController  
        // to be referenced when opening various views 

        rootViewController.bConnection = (netStatus != NotReachable); 
    } 
}
Lauren Quantrell