简体   繁体   中英

iphone Reachability issues

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?

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, eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM