How can I check whether the app is connected to the internet or not?
currently, I am using this code in my appdelegate.m
file
dispatch_queue_t connectivityThread = dispatch_queue_create("com.gm.kart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
if([GMMConnectivity hasConnectivity])
NSLog(@"%@", @"connected");
else
NSLog(@"Not connected");
usleep(10000000);
}
});
and when I click my login button I want to do a check whether the internet is connected or not using NSnotificationcenter
?
Please help me
After download bellow example.
http://developer.apple.com/iphone/library/samplecode/Reachability/index.html
you can use it in your Project like bellow steps:-
included Apple's Reachability.h & .m from their Reachability example.
add the SystemConfiguration framework.
put bellow method in to your appdelegare.m file:-
- (BOOL) connectedToNetwork{
Reachability* reachability = [Reachability reachabilityWithHostName:@"google.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable)
{
isInternet =NO;
}
else if (remoteHostStatus == ReachableViaWWAN)
{
isInternet = TRUE;
}
else if (remoteHostStatus == ReachableViaWiFi)
{ isInternet = TRUE;
}
return isInternet;
}
isInternet is a BOOL declear in to your .h class
as per your code:-
dispatch_queue_t connectivityThread = dispatch_queue_create("com.GMM.assamkart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
isInternet =[self connectedToNetwork];
if (isInternet)
{
NSLog(@"connected");
}
else
{
NSLog(@"Not connected");
}
// usleep(10000000);
}
});
-(BOOL) connectedToInternet
{
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
return ( URLString != NULL ) ? YES : NO;
}