I incorporated the Reachability Code into my app and its been successfully working for a few months but so far I’ve only used it on iOS 5 devices.
However it doesn’t work on an iOS 4 device.
The notification is registered for as follows:
- (id) init
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChange:)
name:kReachabilityChangedNotification
object:nil];
...
- (void) appBecameActive
{
...
self.reachability = [Reachability reachabilityWithHostName:[url host]];
...
[self.reachability startNotifier];
stopNotifier is only getting called in dealloc
The problem is the ReachabilityCallback isn’t getting called back when there is a reachabiity change, but I cannot see any reason why it should it ok on iOS5 but not iOS4.
This part of the code is unchanged from the Apple sample source code:
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) {
#pragma unused (target, flags)
NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
NSCAssert([(__bridge id)info isKindOfClass:[Reachability class]], @"info was wrong class in ReachabilityCallback");
Reachability *noteObject = (__bridge Reachability *)info;
// Post a notification to notify the client that the network reachability changed.
[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
}
As mentioned, everything has been working fine for a long time when running on iOS5 devices, has anybody else encountered a similar issue in the past incorporating the reachability code into with iOS4?
Edit the code in reachability that posts the notification to post it on the main thread, and see if that changes the behavior you’re seeing.
(assume you are listening for them on the main thread)