Reachability responding with wrong status code in iOS 7 iphone 5

北战南征 提交于 2019-12-04 09:05:38

I had the same problem. The solution is to check the flag isConnectionRequired. The documentation says:

WWAN may be available, but not active until a connection has been established.

Code

BOOL isServerAvailable;
Reachability *reachability = [Reachability reachabilityForInternetConnection];

if ((reachability.isConnectionRequired) || (NotReachable == reachability.currentReachabilityStatus)) {
    isServerAvailable = NO;

} else if((ReachableViaWiFi == reachability.currentReachabilityStatus) || (ReachableViaWWAN == reachability.currentReachabilityStatus)){
    isServerAvailable = YES;
}

The old Reachability files are no good. Apple has updated their reachability files.

Check here.

https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html

Download here.

https://developer.apple.com/Library/ios/samplecode/Reachability/Reachability.zip

I encountered this problem and found a solution here. Basically for some reason it's possible to get ReachableViaWWAN even when in airplane mode. However, there's another flag that will indicate whether a connection must first be established. This is the kSCNetworkReachabilityFlagsConnectionRequired flag which has a nice helper method in the Reachability class called connectionRequired

Reachability classes can give strange results if you are using the Network Link Conditioner tool provided by Apple.

 - (void)handleReachability:(Reachability *)reachability
{
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    BOOL connectionRequired = [reachability connectionRequired];
    NSString* statusString = @"";

    switch (netStatus)
    {
        case NotReachable:
        {

            if (connectionRequired) {
                [TSMessage setDefaultViewController:[UIApplication sharedApplication].keyWindow.rootViewController];

                [TSMessage showNotificationWithTitle:NSLocalizedString(@"Something failed", nil)
                                            subtitle:NSLocalizedString(@"The internet connection seems to be down. Please check that!", nil)
                                                type:TSMessageNotificationTypeError];
            }

            connectionRequired = NO;
            break;
        }
        default:
            break;


    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!