I'm using the Apple provided sample code to monitor Reachability - but it's still causing me a headache. I'm running on iOS 5, btw.
// Initialise
hostReach = [[Reachability reachabilityForInternetConnection] retain];
[hostReach startNotifier];
and,
-(void) updateConnectionStatus
{
// Check Internet connectivity
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
if(netStatus == NotReachable)
{
NSLog (@"updateConnectionStatus: network not reachable!");
[self setNetworkAvailable:NO];
}
else
{
NSLog (@"updateConnectionStatus: network reachable!");
[self setNetworkAvailable:YES];
}
}
and also,
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification *) note
{
NSLog (@"reachabilityChanged");
[self updateConnectionStatus];
}
not forgetting,
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog (@"applicationWillEnterForeground");
[self updateConnectionStatus];
}
I've been using this a while, but am wanting to handle reachability in a non-trivial way within the app, for instance using cached data when the network isn't available.
The first problem is that the notification for a change of status does not arrive until around 10th of a second after an app has entered the foreground, and even a manual check when the app re-enters the foreground (as shown above) returns in incorrect result. See this example log from the console,
2012-02-01 13:31:02.566 myapp[9807:707] applicationWillEnterForeground
2012-02-01 13:31:02.632 myapp[9807:707] Reachability Flag Status: -- ------- networkStatusForFlags
2012-02-01 13:31:02.634 myapp[9807:707] updateConnectionStatus: network not reachable!
2012-02-01 13:31:02.660 myapp[9807:707] reachabilityChanged
2012-02-01 13:31:02.662 myapp[9807:707] Reachability Flag Status: WR t----l- networkStatusForFlags
2012-02-01 13:31:02.663 myapp[9807:707] updateConnectionStatus: network reachable!
I have tried both,
hostReach = [[Reachability reachabilityWithHostName:@"www.apple.com"] retain];
and,
hostReach = [[Reachability reachabilityForInternetConnection] retain];
This means that if the network was unreachable when the app last run, the recovery is less efficient that it might be once the app runs again with a network connection.
Is this delay unavoidable?
My other issue is that my app apparently sometimes does not receive these notifications. This seems to happen when my app has been in the background for a while - though I can't recreate this at will.
But sometimes when I come back to the app, the last notification it received was that the network was unreachable, and it apparently never sees a network reachable notification :-(
Anyone else seen anything like that?
Thanks.