5

I have used Andrew's modified Reachability class.

-(void)viewDidLoad

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

When data is downloading I turn AirPort off. But checkNetworkStatus is not being called. Am I missing something. Please help me. This problem driving me nuts. Thanks in advance.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Harsh
  • 666
  • 1
  • 10
  • 21
  • I have exactly the same issue. Fire the notification by myself and the observer get called, but the reachability class never fire this. Apple's demo works good, maybe it's the problem of the modification. :( – Robert Mao Jul 31 '11 at 07:45

4 Answers4

11

Did you tell the reachability instance to start broadcasting notifications?

Reachability *internetReachable = [Reachability reachabilityForInternetConnection];
// This will tell the notifier to start sending notifications
[internetReachable startNotifier];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • 1
    Yes I did this. But still not getting the notifications. If you have code of how to call notification please share it. – Harsh Jul 28 '11 at 13:40
  • 8
    Also another thing to note here, is that the static method reachabilityForInternetConnection always creates new instance of Reachability object. So make sure you keep a pointer to that, otherwise you will not receive the notification of change, since object will already be released. – Legoless Jul 31 '13 at 01:28
  • 1
    @legoless this is only true in ARC, this question was asked before ARC was available. – rckoenes Jul 31 '13 at 07:15
2

This worked for me (Swift):

func handleNetworkChange(notification:NSNotification) { ... }

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("handleNetworkChange:"), name: kReachabilityChangedNotification, object: nil)
ReachabilityChecker = Reachability.reachabilityForInternetConnection()
ReachabilityChecker!.startNotifier()
sabiland
  • 2,526
  • 1
  • 25
  • 24
2

put it in this sequence

in ur view did load

First register

then

post that notification

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


[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:nil];
2

I have exactly the same issue, Apple's example works great, so I end up replace Reachability class with Apple's version, everything works great. This costed me almost 2 hours.

Simply replace your Reachability.h, .m from Apple's example, everything should just worked.

Robert Mao
  • 1,911
  • 22
  • 24