iOS check for internet connection

Thanks to Tony Million there is ARC and GCD Compatible Reachability Class for iOS and MacOS from the original version developed as an example by apple. All you have to do is download the zip file for the Reachability project from the GitHub repository and then copy the Reachability.h and Reachability.m into your xcode application. Don't forget to add Reachability.m to your compile sources and SystemConfiguration.framework library in your project Build Phases. Here is the link to the GitHub project https://github.com/tonymillion/Reachability

iOS check for internet connection iOS internet connection Reachability class

Interface file for the App Delegate - CheckNetworkAppDelegate.h

#import <UIKit/UIKit.h>

@class CheckNetworkViewController;

@interface CheckNetworkAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) CheckNetworkViewController *viewController;

@end

Implementation file for the App Delegate - CheckNetworkAppDelegate.m

#import "CheckNetworkAppDelegate.h"
#import "CheckNetworkViewController.h"

@implementation CheckNetworkAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.viewController = [[CheckNetworkViewController alloc]
                           initWithNibName:@"CheckNetworkViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

@end

Interface file for the View Controller - CheckNetworkViewController.h

#import <UIKit/UIKit.h>

@interface CheckNetworkViewController : UIViewController

@end

Implementation file for the View Controller - CheckNetworkViewController.m

#import "CheckNetworkViewController.h"
#import "Reachability.h"


@interface CheckNetworkViewController ()

- (void)networkChanged:(NSNotification *)notification;

@end

@implementation CheckNetworkViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
 self.view.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                    selector:@selector(networkChanged:)
                                    name:kReachabilityChangedNotification object:nil];
    
    Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
    [reachability startNotifier];
    
}

- (void)networkChanged:(NSNotification *)notification
{
    
    Reachability * reachability = [notification object];
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
    
    if(remoteHostStatus == NotReachable) {
        NSLog(@"Not Reachable");
    }
    else if (remoteHostStatus == ReachableViaWiFi) {
        NSLog(@"Reachable Wifi");
    }
    else if (remoteHostStatus == ReachableViaWWAN) {
        NSLog(@"Reachable WWAN");
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


4 comments :

  1. The next night the pair fifa 14 coins came to rest at the house of a very poor, but very hospitable farmer cheap fifa 14 coins and his wife. After sharing what little food they had the couple let the angels sleep in their fifa 14 coins bed where they could have a good night's rest.

    ReplyDelete

  2. The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
    hotmail sign in | red ball 1

    ReplyDelete
  3. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks.
    tanki online 3 | 2048 online

    ReplyDelete