I am using Apple's Reachability class, and have written a simple test app to just surface the events it picks up.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    [self initializeNetworkReachability];
    ...
}

#pragma mark - Network Reachability/Connection methods

- (void)initializeNetworkReachability {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkConnection:) name:kReachabilityChangedNotification object:nil];
    internetAvailable = [Reachability reachabilityForInternetConnection];
    [internetAvailable startNotifier];
    [self checkConnection:nil];
}

- (void)checkConnection:(NSNotification *)notification {
    NetworkStatus internetStatus = [internetAvailable currentReachabilityStatus];

    switch (internetStatus) {
        case NotReachable:
            _isConnectedToInternet = NO;
            break;
        case ReachableViaWiFi:
            _isConnectedToInternet = YES;
            break;
        case ReachableViaWWAN:
            _isConnectedToInternet = YES;
            break;
        default:
            _isConnectedToInternet = NO;
            break;
    }

    [self connectionAchieved:_isConnectedToInternet];
}

- (void)connectionAchieved:(BOOL)achieved {
    _isConnectedToInternet = achieved;
}

All pretty plain vanilla... but herein lies the rub. If I go to a place where I know my connectivity is completely useless (a place on the street just outside my office building), I do not get a network reachability changed notification. However, if I open Safari and try browsing a website, it is impossible to reach and just hangs trying to load the page.

I went as far as wandering down the street watching my WiFi indicator and cellular signal strength, which changed but I never got a signal from the reachability stuff.

Finally, I went into a store, walked to the very back, and watched WiFi switch to cellular 1x (I am on an iPhone 5), and finally got a reachability change (no network available). The entire time the network was completely unusable.

Am I doing something wrong? Misinterpreting/Misunderstanding the usage of Reachability? Or, if not, how in the world do people handle these conditions??

有帮助吗?

解决方案

The behavior that you are describing is exactly how I would expect the Reachability API to behave. Its basically a way to simply know if your device is connected over WiFi, cellular or has no connection at all. It gives no indication of the signal strength or the quality of the network connection.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top