How to check internet connection is ON in iOS Device?

Introduction – Purpose of Checking Internet Connection of iOS Device

During Mobile App building before integrating any feature first we thought about its circumstance or crashes it will gonna face and one of common issue is internet connection, let us suppose if our feature or third party library function calling or any other class is called which needs internet connection mandatory then if in case that internet connection is not available or it is turned off in device.

Check Internet Connection iOS Device iPhone or iPad

Overview on Checking Internet Connection of iOS Device

For checking of internet Connectivity you need to know about a class file that is Reachability and you can get this file and its details information from this link :- https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html . Now if we talk about internet connectivity there are mainly two methods of internet connection in device are WiFi or default internet. Make sure that you have already understand the class reachability and its functions that are going to be used in our purpose.

SOLUTION for Checking Internet Connection of iOS Device

First of all download or copy paste files of Reachability class into your project and after adding that now you can lead to code part :-

Firstly, import Reachability class in your .h file
#import "Reachability.h”

After that, declare a variable of this class
@property (nonatomic, strong) Reachability *wifiReach;
@property HTTPResponseCode responseCode;

Now, in .m file add function init in it,

- (id)init {
self = [super init];
if(self) {
wifiReach = [Reachability reachabilityWithHostName:@"www.google.com"];
[wifiReach startNotifer];
}
return self;
}

And after that add

#pragma -
#pragma Network Status Update
- (void) reachabilityChanged: (NSNotification* )note {
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
if(currentNetworkStatus == netStatus) {
return;
}
currentNetworkStatus = netStatus;
}

- (BOOL)isReachable {
NetworkStatus networkStatus = [wifiReach currentReachabilityStatus];
if(networkStatus == NotReachable) {
return NO;
}
return YES;
}

Now, you can check your iPhones internet connectivity by calling method isReachable wherever you want in your code and it will return YES or NO as status of internet connectivity.

Leave a Comment