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

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.