Question

I have implemented reach-ability in my project that check the connection is reachable or not at some specific time example when go for the any network connection task. But what i want is to check the network continuously if it is reachable the show the notification but how to implement it i do not know so please help

Was it helpful?

Solution

Use this Code to Check Network Connection available or not in Device Using Reachability

@interface appDelegate : UIResponder <UIApplicationDelegate>
{
    Reachability *internetReachable;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 ........

 internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];
 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(checkNetworkStatus:)
                                                 name:kReachabilityChangedNotification object:nil];
............

}

- (void)checkNetworkStatus:(NSNotification *)notice {
    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            break;
        }
        case ReachableViaWiFi:
        {
             NSLog(@"The internet is Connected.");
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN!");
            break;
        }
    }
}

//#import "Reachability.m"

static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
    NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
    NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");

    Reachability* noteObject = (__bridge Reachability *)info;
    // Post a notification to notify the client that the network reachability changed.
    [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
}

it's Solve your problem.

OTHER TIPS

Just call this in your App Delegate :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self testInternetConnection];

    return YES;
}   

- (void)testInternetConnection
{
    internetReachability = [Reachability reachabilityWithHostname:@"www.google.com"];

    UIAlertView *alertForInternet = [[UIAlertView alloc]initWithTitle:@"Internet Connection Established." message:Nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

    internetReachability.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [alertForInternet setTitle:@"Internet Connection Established."];
            [alertForInternet setMessage:Nil];
            [alertForInternet show];


        });
    };

    // Internet is not reachable
    internetReachability.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Someone broke the internet :(");
            [alertForInternet setTitle:@"Internet Connection Lost."];
            [alertForInternet setMessage:@"Please Check the Connection"];
            [alertForInternet show];

        });
    };

    [internetReachability startNotifier];
}

Here

[internetReachability startNotifier]; will take care of the rest.

Use a timer. Set the timer to check for connection availability in small intervals, probably 1 sec. The way you check for this kind of activity is usually by using timers. Other option would be to find an event that notifies that this has occurred.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top