2012-04-17 8 views
6

Ho difficoltà a utilizzare il livello di accesso nel mio codice. Mi piacerebbe mantenerlo molto semplice avviando un osservatore al momento del lancio e poi semplicemente ricevendo le notifiche di modifica. Nel seguente codice, il metodo RaggiungibilitàChanged non viene mai chiamato. Ho provato molte iterazioni ma questa è la versione più semplice. Compila e funziona. Si prega di aiuto ...Notifica di estendibilità mai chiamata

** * * codice AppDelegate.h * ***

#import <UIKit/UIKit.h> 

#ifdef PHONEGAP_FRAMEWORK 
    #import <PhoneGap/PGViewController.h> 
    #import <PhoneGap/PGURLProtocol.h> 
    #import <PhoneGap/Reachability.h> 
#else 
    #import "PGViewController.h" 
    #import "PGURLProtocol.h" 
    #import "Reachability.h" 

#endif 

@interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, PGCommandDelegate> { 
    NSString* invokeString; 
} 

@property (nonatomic, copy) NSString* invokeString; 
@property (nonatomic, strong) IBOutlet UIWindow* window; 
@property (nonatomic, strong) IBOutlet PGViewController* viewController; 

@end 

** * * Snippet di codice AppDelegate.m * ***

#import "AppDelegate.h" 

#import "MainViewController.h" 

#ifdef PHONEGAP_FRAMEWORK 
    #import <PhoneGap/PGPlugin.h> 
    #import <PhoneGap/PGURLProtocol.h> 
    #import <PhoneGap/Reachability.h> 
#else 
    #import "PGPlugin.h" 
    #import "PGURLProtocol.h" 
    #import "Reachability.h" 

#endif 

@implementation AppDelegate 
@synthesize invokeString, window, viewController; 

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

    NSLog(@"???????? CODE NEVER GETS HERE ??????????"); 

    Reachability *reach = [notice object]; 
    NSParameterAssert([reach isKindOfClass: [Reachability class]]); 
    NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) {NSLog(@"**** Not Reachable ****");} 
    else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"**** wifi ****"); } 
    else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"**** cell ****"); } 
} 

- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 
{  

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

    Reachability *reach = [Reachability reachabilityForInternetConnection]; 
    [reach startNotifier]; 

    NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; 

    NSLog(@”???? ALWAYS INITS WITH Not Reachable ????”); 
    if(remoteHostStatus == NotReachable) {NSLog(@"init **** Not Reachable ****");} 
    else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"int **** wifi ****"); } 
    else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"init **** cell ****"); } 

    // ... 

} 

@end 
+0

Sicuramente il titolo e/o i tag possono essere aggiornati per rendere questa domanda più facile da identificare ... –

+0

Just edited - si spera che il titolo e i tag sono più descrittivi. –

risposta

26

vostro raggiungibilità è AutoRelease quindi è dealloc e non funziona più.

provo il codice e si sta lavorando per me:

codice AppDelegate.h

[...] 
@property (retain, nonatomic) Reachability* reach; 
[...] 

codice AppDelegate.m frammento di

[...] 
@synthesize reach; 

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

    NSLog(@"!!!!!!!!!! CODE IS CALL NOW !!!!!!!!!!"); 

    NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) {NSLog(@"**** Not Reachable ****");} 
    else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"**** wifi ****"); } 
    else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"**** cell ****"); } 
} 

- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 
{  

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

    self.reach = [Reachability reachabilityForInternetConnection]; //retain reach 
    [reach startNotifier]; 

    NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; 

    NSLog(@"???? ALWAYS INITS WITH Not Reachable ????"); 
    if(remoteHostStatus == NotReachable) {NSLog(@"init **** Not Reachable ****");} 
    else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"int **** wifi ****"); } 
    else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"init **** cell ****"); } 

    // ... 

} 

[...] 
-(void)dealloc{ 
    [reach release]; 
    [super dealloc]; 
} 

@end 
+0

Penso sia! Ho provato le tue modifiche attivando la Modalità aereo e vedo i messaggi di registro corretti. Grazie così tanto! –

+0

@Sarah Moreland Prego. Dato che sei un nuovo utente, mi permetto di ricordarti di votare la mia risposta se ti piace e/o di accettare la risposta se risolvesse il tuo problema. Il pulsante è in alto a sinistra nella mia risposta –

+0

Grazie, ho controllato la risposta di accettazione ma non riesco a votare perché non ho una reputazione di 15. Lo farei se potessi. È un'ottima risposta. –