How to check network status in iphone app?

I have some methods for checking network status in my application.

In my viewDidLoad method viewDidLoad I call initNetworkCheck :

 [self initNetworkCheck]; [super viewDidLoad]; if(internetActive == NO){ compose.enabled = NO; } 

Therefore, I want to check the launch if the equipment has an Internet connection. The problem is that it always gives me NO, but internetActive is actually YES when I load it.

 //[[[[[[network check methods -(void)checkNetworkStatus:(NSNotification *)notice{ NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable:{ self.internetActive = NO; break; } case ReachableViaWiFi:{ self.internetActive = YES; break; } case ReachableViaWWAN:{ self.internetActive = YES; break; } } NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; switch (hostStatus){ case NotReachable:{ self.hostActive = NO; break; } case ReachableViaWiFi:{ self.hostActive = YES; break; } case ReachableViaWWAN:{ self.hostActive = YES; break; } } } -(void)initNetworkCheck{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; internetReachable = [[Reachability reachabilityForInternetConnection] retain]; [internetReachable startNotifier]; hostReachable = [[Reachability reachabilityWithHostName:@"www.google.com"] retain]; [hostReachable startNotifier]; } //]]]]]] 

Any ideas?

+4
source share
2 answers

I suggest using the Apple Reachability class. Here is an example of an Apple application.

A sample productivity application demonstrates how to use the SystemConfiguration Frame to monitor the network status of an iPhone or iPod touch. In particular, this demonstrates how to know when IP can route and when traffic will be routed through a wireless broadband network (WWAN) interface, such as EDGE or 3G.

+1
source

I personally use the following:

 typedef enum { NoConnection = 0, WiFiConnected, WWANConnected } NetworkStatus; NetworkStatus getNetworkStatus ( ) { struct sockaddr_in nullAddress; bzero(&nullAddress, sizeof(nullAddress)); nullAddress.sin_len = sizeof(nullAddress); nullAddress.sin_family = AF_INET; SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*) &nullAddress); SCNetworkReachabilityFlags flags; SCNetworkReachabilityGetFlags(ref, &flags); if (!(flags & kSCNetworkReachabilityFlagsReachable)) return NoConnection; if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired)) return WiFiConnected; if (((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) || (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)) && !(flags & kSCNetworkReachabilityFlagsInterventionRequired)) return WiFiConnected; if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) return WWANConnected; return NoConnection; } 

I forget where exactly, but somewhere in the SDK there is an example on which it is based.

EDIT: looks like Nick found him ... :)

+3
source

Source: https://habr.com/ru/post/1348076/


All Articles