views:

324

answers:

1

My app requires an internet connection, so in the ApplicationDelegate, on applicationDidFinishLaunching I am running the following:

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

hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReach startNotifer];
[self updateInterfaceWithReachability: hostReach];

But for some reason, this seems to be firing two times, since what gets logged is the following:

2010-02-04 14:25:48.004 myApp[201:207] Reachability Flag Status: -- ------- networkStatusForFlags
2010-02-04 14:25:48.240 myApp[201:207] STATUS: Access Not Available
2010-02-04 14:25:48.499 myApp[201:207] Reachability Flag Status: -- ------- networkStatusForFlags
2010-02-04 14:25:48.517 myApp[201:207] STATUS: Access Not Available

Which is good that it's working, but I have an alert message to notify the user that there is no connection, and it pops up twice...

Why is the reachability notifier firing two times?

A: 

No idea why it's popping up twice. Have you tried commenting out the call to startNotifier? It might do an initial check in the reachabilityWithHostName: method.

This might be a case where a workaround is the most appropriate fix, e.g.:

static bool userNotifiedOfReachability = NO;

...

- (void)updateInterfaceWithReachability:(Reachbility *)reachability {
    if (!userNotifiedOfReachability) {
        // Notify User

        ...

        userNotifiedOfReachability = YES;
    }
}

That would be appropriate for a "please try launching the app again later" message.

Frank Schmitt
That worked...almost. It's always the second time that confirms whether the connection is made, this is checking and ignoring the second time. I got it working though, thanks for the tip!
rson