2010-05-26 3 views
0

Il existe tellement d'informations sur l'utilisation de l'exemple d'accessibilité d'Apple, et tant de choses sont contradictoires. J'essaie de trouver sur je l'utilise (Reachability 2.0) correctement ci-dessous. Mon cas d'utilisation d'application est la suivante: Si une connexion Internet est disponible par tous les moyens (wifi, LAN, Edge, 3G, etc.) un UIButton ("Voir plus") est visible sur diverses vues. Si aucune connexion, le bouton n'est pas visible. La partie "Voir plus" n'est en aucun cas critique pour l'application, c'est juste une fonctionnalité supplémentaire. "Voir plus" pourrait être visible ou non à tout moment pendant le cycle de vie de l'application lorsque la connexion est établie ou perdue. Voici comment je l'ai fait - Est-ce correct et/ou existe-t-il un meilleur moyen?Recherche d'accessibilité (2.0) Validation de cas d'utilisation

Toute aide est grandement appréciée! 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 

Répondre

1

Pourquoi ne pas rootViewController.bConnection = (netStatus != NotReachable);, qui continue de fonctionner si elles ajoutent un autre type de connectivité? Ressemble à une erreur (c'est équivalent à (signed char *)). En général, je préfère bool C99 et la spécification C99 permet spécifiquement bool blah:1; si vous êtes préoccupé par l'espace. De plus, soyez prudent avec l'idée que vous pouvez vérifier une "connexion Internet" - vous pouvez vérifier une connexion réseau, mais votre FAI pourrait être en panne ou l'hôte auquel vous voulez vous connecter pourrait être en panne, ou quelqu'un aurait pu percer un câble sous-marin. Si kHostName est le nom d'hôte auquel vous vous intéressez, alors il fera ce qu'il faut.

0

Merci pour la réponse rapide! Donc ce que je rassemble est tout ce que je dois faire est de changer le dessus à:

- (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); 
    } 
} 
Questions connexes