Search code examples
iosiphoneipadafnetworkingnsnotificationcenter

Check Internet connection state using AFNetworking


I want to create common class for checking internet connection also it should notify me when internet changes its status.

I am using AFNetworking for checking internet status.

Here is my code i have tried but it is not working please help where i am making mistake??

CheckInternet is my common class derived from NSObject

CheckInternet.h

#import <Foundation/Foundation.h>

@interface CheckInternet : NSObject

+ (void)startNetworkMonitoring;
+ (BOOL)isInternetConnectionAvailable;
@end

CheckInternet.m

#import "CheckInternet.h"
#import "AFNetworking.h"

@implementation CheckInternet


+ (void)startNetworkMonitoring
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));

        // Check the reachability status and show an alert if the internet connection is not available
        switch (status) {
            case -1:
                // AFNetworkReachabilityStatusUnknown = -1,
                NSLog(@"The reachability status is Unknown");
                break;
            case 0:
                // AFNetworkReachabilityStatusNotReachable = 0
                NSLog(@"The reachability status is not reachable");
                break;
            case 1:
                NSLog(@"The reachability status is reachable via wan");
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:nil];
                break;
            case 2:
                // AFNetworkReachabilityStatusReachableViaWiFi = 2
                NSLog(@"The reachability status is reachable via WiFi");

                [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:nil];
                break;

            default:
                break;
        }

    }];
}

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

@end

In My ViewController.m (here i want to check internet status)

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];



     [CheckInternet startNetworkMonitoring];


    if ([CheckInternet isInternetConnectionAvailable])
    {
         NSLog(@"---- Internet YES------ ");
    }
    else
    {
         NSLog(@"----- Internet NO------ ");
    }
}

//This is not called...
- (void)reachabilityChanged:(NSNotification *)notification
{
    NSLog(@"---- reachabilityChanged ----");
}

Thank you in advance!


Solution

  • First, I would recommend to start monitoring your network activity in the AppDelegate. Move [CheckInternet startNetworkMonitoring]; to didFinishLaunchingWithOptions

    Second, add the following line to case == 0:

     [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:[NSNumber numberWithInteger:one]];
    

    Also make sure, you post the status of your connection with the notification. Because it is an integer, you need to wrap it in an object (typically into NSNumber).

    Third, you need to observe the reachabilityChanged notification. Add this in your viewDidLoad.

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

    In your function - (void)reachabilityChanged:(NSNotification *)notification you should be able to access the notification's userInfo property and read the state code there.

    Also, do not forget to unobserve the notification once your viewController has disappeared. So in dealloc

    [NSNotificationCenter defaultCenter] removeObserver:self];