Notification is not being called in Reachability
Asked Answered
G

4

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.

Grenadines answered 28/7, 2011 at 13:14 Comment(1)
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. :(Improbability
M
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];
Maragretmarala answered 28/7, 2011 at 13:17 Comment(1)
make sure ur kReachabilityChangedNotification defined.nslog it.it should be nsstringMaragretmarala
S
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];
Sneaky answered 28/7, 2011 at 13:28 Comment(3)
Yes I did this. But still not getting the notifications. If you have code of how to call notification please share it.Grenadines
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.Moldau
@legoless this is only true in ARC, this question was asked before ARC was available.Sneaky
M
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];
Maragretmarala answered 28/7, 2011 at 13:17 Comment(1)
make sure ur kReachabilityChangedNotification defined.nslog it.it should be nsstringMaragretmarala
I
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.

Improbability answered 31/7, 2011 at 8:50 Comment(0)
P
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()
Perfidy answered 10/10, 2014 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.