Looking for Reachability (2.0) Use Case Validation

Posted by user350243 on Stack Overflow See other posts from Stack Overflow or by user350243
Published on 2010-05-26T12:41:50Z Indexed on 2010/05/26 14:21 UTC
Read the original article Hit count: 281

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about iphone

Related posts about xcode