Use Apple's default reachability class:
Download reachability project from This link
Copy Reachability.h and Reachability.m file in your project.
And set this method in Application delegate file.
-(void)initializeRechabilityObeserver
{
//Change the host name here to change the server your monitoring
hostReach = [Reachability reachabilityWithHostName: @"www.apple.com <http://www.apple.com>"];
[hostReach startNotifier];
//[self updateInterfaceWithReachability: hostReach];
internetReach = [Reachability reachabilityForInternetConnection];
[internetReach startNotifier];
//[self updateInterfaceWithReachability: internetReach];
wifiReach = [Reachability reachabilityForLocalWiFi] ;
[wifiReach startNotifier];
//[self updateInterfaceWithReachability: wifiReach];
}
For getting Reachability change notification use below code:
Add this notification method in Application didFinishLaunching
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
internetReachable = [Reachability reachabilityForInternetConnection] ;
[internetReachable startNotifier];
}
and also add this method:
- (void)reachabilityChanged: (NSNotification* )note
{
NSLog(@"Reachability changed");
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
}