Search code examples
iosreachability

Reachability detect net status, I want to remove the address


if the google.com don't work! but the net is ok. the code will bring error. so I want to remove the "google.com" to detect the status of net .

I can't find the method of Reachability without address.

#import "Reachability.h"

static  ReachabilityCenter *reachCenter;
@implementation ReachabilityCenter

@synthesize reachablity;
@synthesize isConnectedNet;

+(id)shareCenter
{
    if(!reachCenter)
    {
         reachCenter = [[ReachabilityCenter alloc] init];
         reachCenter.reachablity = [Reachability reachabilityWithHostname:@"www.google.com"];
    }
    return reachCenter;
}

-(void)reachabilityChanged:(NSNotification*)note
{
    Reachability * reach = [note object];
    isConnectedNet = [reach isReachable];
}

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

    [reachCenter.reachablity  startNotifier];

}
@end

I test the code detect the status without website,ip.

-(void)detectNetStatus
{
    Reachability *r =  [Reachability reachabilityForLocalWiFi];
    isConnectedNet = [r currentReachabilityStatus];
}

-(NetworkStatus)isConnectedNet
{
    [self detectNetStatus];
    return isConnectedNet;
}

Solution

  • What you are asking is actually impossible and here is why: You can't validate that the network connection is active "for sure" (in the way that you want) without using it. In order to use it, you have to use it for something which means a connection with an expected resource X, Y or Z.

    As an example, consider this: What if you are connected to a local network that doesn't have external internet access? It would look like a connection is available, and in fact it does --- it just isn't what you had in mind.

    So you have to test against X, Y or Z to validate what you want to access is available.

    Added info:

    You can determine if a network connection is available:

    Look for: Reachability* internetReach;

    In Apple's example: http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_ReachabilityAppDelegate_h.html#//apple_ref/doc/uid/DTS40007324-Classes_ReachabilityAppDelegate_h-DontLinkElementID_3

    The problem is --- just because a network connection is available, and the above internetReach will --- it doesn't tell you anything about the connection. It could just be a local network. You can't find out until you try to connect something specific you expect to be available like, say, Apple.com or Google.com