Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Detect network change using Reachability in ios

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.12k
    Comment on it

    Following code will help you to detect the change in network availability.
    Download the reachablity class from here :

    https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

    #import "Reachability.h"
    

    Then put following code in your App delegate didFinishLaunchingWithOptions method

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        { 
        Reachability *reachability = [Reachability reachabilityForInternetConnection];
         [reachability startNotifier];
    NetworkStatus status = [reachability currentReachabilityStatus];
    
    if(status == NotReachable) 
    {
    
        //No internet
    }
    else if (status == ReachableViaWiFi)
    {
        //WiFi
    }
    else if (status == ReachableViaWWAN) 
    {
        //3G
    }
        [[NSNotificationCenter defaultCenter] postNotificationName:notifcationstr object:status]
    
        }
    

    Set Notification Observer

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NetworkreachablityChanging:) name:kReachabilityChangedNotification object:nil];
    
    
    - (void)NetworkreachablityChanging:(NSNotification *)notification {
        Reachability *reachability = (Reachability *)[notification object];
    
        if ([reachability isReachable]) {
            NSLog(@"online");
        } else {
            NSLog(@"offline");
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: