-2

I want to call a method when internet is active . But my method is call but it execute two time. I am unable how it is hapenning. Please help .Here is my code.

Reachability * reachability;
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChanged:) name:kReachabilityChangedNotification object:nil];
        - (void)applicationDidBecomeActive:(UIApplication *)application {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

            [self handleNetworkChanged:nil];
        }

        - (BOOL)handleNetworkChanged:(NSNotification *) notice
        {
            reachability = [Reachability reachabilityForInternetConnection];
            [reachability startNotifier];
            NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
            if(remoteHostStatus == NotReachable) {
                self.internetConnection = FALSE;

                  [[NSNotificationCenter defaultCenter] postNotificationName:@"NetworkGone" object:nil];
                //NSLog(@"Internet is not Connected");
            } else {
                self.internetConnection = TRUE;

                 [[NSNotificationCenter defaultCenter] postNotificationName:@"NetworkCome" object:nil];
                //NSLog(@"Internet is Connected");
            }
            return  self.internetConnection;
        }
priyanka gautam
  • 377
  • 4
  • 15

1 Answers1

-2

Remove [self handleNetworkChanged:nil] from applicationDidBecomeActive.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChanged:) name:kReachabilityChangedNotification object:nil];

- (void)applicationDidBecomeActive:(UIApplication *)application {

    }

- (BOOL)handleNetworkChanged:(NSNotification *) notice
{
    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
    if(remoteHostStatus == NotReachable) {
        self.internetConnection = FALSE;

          [[NSNotificationCenter defaultCenter] postNotificationName:@"NetworkGone" object:nil];
        //NSLog(@"Internet is not Connected");
    } else {
        self.internetConnection = TRUE;

         [[NSNotificationCenter defaultCenter] postNotificationName:@"NetworkCome" object:nil];
        //NSLog(@"Internet is Connected");
    }
    return  self.internetConnection;
}
darshan
  • 1,115
  • 1
  • 14
  • 29