I use reacheability class for checking my network connectivity in my app...

Reachability *reach = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus netStatus = [reach currentReachabilityStatus];    
    if (netStatus==NotReachable)
    {
        NSLog(@"NR");
    }

I need to find when the network status change (i.e when the network status changes from reachable to notreachable and vice versa).

Is there any delegates to find this thinks, Any suggestions ?

有帮助吗?

解决方案 2

Used this flag kReachabilityChangedNotification to find the change in network status and passed that to a NSNotificationCenter

Here is the code:

NSString *host = @"https://www.apple.com"; // Put your host here

        // Set up host reach property
       hostReach = [Reachability reachabilityWithHostname:host];

                          // Enable the status notifications
                          [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
                           [hostReach startNotifier];

   - (void) reachabilityChanged: (NSNotification* )note
{
    Reachability *reachability = [note object];
    NSParameterAssert([reachability isKindOfClass:[Reachability class]]);
    if (reachability == hostReach) {
        Reachability *reach = [Reachability reachabilityForInternetConnection]; 
        NetworkStatus netStatus = [reach currentReachabilityStatus];    
        if (netStatus==NotReachable)
        {
            NSLog(@"notreacheable");
        }
        else {
            NSLog(@"reacheable");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"startUpdatingTable" object:nil];
        }
    }
}

其他提示

I suggest to make use of Apple's Reachability class. Here is a sample App by Apple.

and also check this links.

http://www.switchonthecode.com/tutorials/iphone-snippet-detecting-network-status

use Reachability" class

add flooding method in app delegate so you can use this method any ware in project

#import "Reachability.h"
-(BOOL)isHostAvailable
{
    //return NO; // force for offline testing
    Reachability *hostReach = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [hostReach currentReachabilityStatus];
    return !(netStatus == NotReachable);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top