Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify network connectivity in objective-c

I was looking over the Reachability sample project on developer.apple.com and found that it was a large project just to verify you had network connectivity.

First part of the question is "What is the minimum code required to find out if the device can reach a 3G or wifi network?"

And next should this be done inside the appDelegate (on start) or inside the first View Controller that is launched?

Thank you in advance

like image 867
JimmyBond Avatar asked Feb 15 '11 19:02

JimmyBond


People also ask

How do I check my Alamofire internet connection?

You can use NetworkReachabilityManager class from Alamofire and configure the isConnectedToInternet() method as per your need. I am only checking if the device is connected to internet or not. print("Yes! internet is available.")

How do I check my network connection on iPad?

Open the Wifi app. Tap the icon tab, then Network check. On the network check screen, tap Test Wi-Fi. We'll test one Wifi point at a time, showing speeds for each device connected to that Wifi point.

How do I check my network on iOS?

To check if your iOS mobile device is connected to Wi-Fi:From the main screen of your device, look for and open Settings. With Settings open, look for the Wi-Fi field. Off - the Wi-Fi antenna is currently disabled. Not Connected - Wi-Fi is turned on, but your device is not currently connected to a network.


1 Answers

It's not large, it really does what you want. If it is too big for you, you can extract what you need only like reachabilityForLocalWiFi. But I'm afraid that it will not be much smaller.

Yes, you can use reachability in your application delegate or inside the first view controller.

Reachability notification registration ...

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(networkReachabilityDidChange:)
                                             name:kReachabilityChangedNotification
                                           object:nil];
__reachability = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[__reachability startNotifier];

... callback method example ...

- (void)networkReachabilityDidChange:(NSNotification *)notification {
  Reachability *reachability = ( Reachability * )[notification object];
  if ( reachability.currentReachabilityStatus != NotReachable ) {
    // Network is available, ie. www.google.com
  } else {
    // Network is not available, ie. www.google.com
  }
}

... do not forget to stop notifications, remove observer and release rechability object.

like image 82
zrzka Avatar answered Sep 30 '22 17:09

zrzka