Reputation: 463
I have an app that displays the network availability whenever there is some activity such as the view change. However i am looking for a code that runs in the background to check the network availability even if i will be idle on the same screen and it must display a message that "network is not available/network is available"
Upvotes: 2
Views: 274
Reputation: 73688
I use this piece of code to detect network availability. I basically declare local vars to figure out the things like whether I am on WiFi or 3G network or whether internet is available.
Notifications come at some intervals & update these variables. You access these BOOL vars to know the status.
- (void)checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch(internetStatus)
{
case NotReachable:
{
//NSLog(@"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
//NSLog(@"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
//NSLog(@"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
//NSLog(@"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
//NSLog(@"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
//NSLog(@"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
return;
}
Upvotes: 2