Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reachability Notification Never Called

I am having a hard time using Reachability in my code. I would like to keep it very simple by initiating an observer at launch and then just receiving change notifications. In the following code, the reachabilityChanged method is never called. I’ve tried many iterations but this is the simplest version. It compiles and runs. Please help...

**** AppDelegate.h code ****

#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

**** AppDelegate.m code snippet ****

#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
like image 954
Sarah Moreland Avatar asked Apr 17 '12 01:04

Sarah Moreland


1 Answers

Your object Reachability is autorelease so it's dealloc and not working anymore.

I try your code and it's working for me:

AppDelegate.h code

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

AppDelegate.m code snippet

[...]
@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
like image 101
Martin Magakian Avatar answered Oct 16 '22 10:10

Martin Magakian