Member-only story

Checking for network connectivity iOS

Mr.Javed Multani
1 min readOct 3, 2020

--

Creating a Reachability listener

Apple’s Reachability class periodically checks the network status and alerts observers to changes.

Reachability *internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];

Add observer to network changes

Reachability uses NSNotification messages to alert observers when the network state has changed. Your class will need to become an observer.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

Elsewhere in your class, implement method signature

- (void) reachabilityChanged:(NSNotification *)note {
//code which reacts to network changes}

Alert when network becomes unavailable

- (void)reachabilityChanged:(NSNotification *)note {
Reachability* reachability = [note object];
NetworkStatus netStatus = [reachability currentReachabilityStatus];if (netStatus == NotReachable) {
NSLog(@"Network unavailable"); }}

Alert when connection becomes a WIFI or cellular network

- (void)reachabilityChanged:(NSNotification *)note {
Reachability* reachability = [note object];
NetworkStatus netStatus = [reachability currentReachabilityStatus];switch (netStatus) {
case NotReachable:NSLog(@"Network unavailable");break;
case ReachableViaWWAN:NSLog(@"Network is cellular");break;
case ReachableViaWiFi:NSLog(@"Network is WIFI");break;
}}

--

--

Mr.Javed Multani
Mr.Javed Multani

Written by Mr.Javed Multani

Software Engineer | Certified ScrumMaster® (CSM) | UX Researcher | Youtuber | Tech Writer

No responses yet