Friday, 13 March 2015

Check Internet Connectivity Using AFNetworking 2.0

I have created NetworkRechabilityMonitor class to get the Internet Connection status in my app using AFNetworking 2.0 library.

1. Create an NSObject class naming NetworkRechabilityMonitor, add below code in respective Header & Implementation files.


#import <Foundation/Foundation.h>

@interface NetworkRechabilityMonitor : NSObject




#import "NetworkRechabilityMonitor.h"

@implementation NetworkRechabilityMonitor

#pragma mark - Start Monitoring Network Manager
+(void)startNetworkReachabilityMonitoring {
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];

#pragma mark - Check Internet Network Status
+(BOOL)checkNetworkStatus {
    return [AFNetworkReachabilityManager sharedManager].reachable;


2. Import the NetworkRechabilityMonitor in your ViewController class where you want to check the internet status.

3. In viewDidLoad method of your ViewController add below line of code:

[NetworkRechabilityMonitor startNetworkReachabilityMonitoring];

4. Now in ViewController if i want to check internet status before making an API call add below code:
if ([NetworkRechabilityMonitor checkNetworkStatus]) {
          NSLog(@"Connected to Internet");
          // Add your code
} else {
         NSLog(@"Not Connected to Internet");
         // Notify user to connect to Internet

No comments:

Post a Comment