-1

in my application i want to implement a functionality, i have an button in my application and i want when network is loss from iOS device then this button should automatically become disable, and when again get the network available properly then button should become enable.

this should be done automatically every time when device network connection loss or become available.

i am not getting any way or any type of clue to do achieve this functionality, if anybody know how to implement this feature in iOS application then please help me.

Thank in advance.

Chetan sharma
  • 378
  • 5
  • 21

3 Answers3

4

I believe that you are using Reachability class for network status detection. So you can register for the network status change detection with this code snippet

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

Now you can call startNotifier for you reachability object

[self.internetReachability startNotifier];

and inside the method reachabilityHasChanged you can capture the status change for all 3 states i.e. ReachableViaWiFi, ReachableViaWWAN and NotReachable. Now from not reachable case you can disable the button like:

myButton.enabled = NO;

and from reachable states you can enable it again.

Gandalf
  • 2,399
  • 2
  • 15
  • 19
1

You can use a NSTimer and in that timer you can call a BOOL function of Reachability class after every 2 seconds and make it repeat YES which returns true of false

NSTimer* internetTimer;
 internetTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(checkInternet) userInfo:nil repeats:YES];

-(void)checkInternet{
    if([self isInternetAvailable])
     {
           //enable button
     }
     else
    {
          //disable button
    }

}

-(BOOL)isInternetAvailable()
{
    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);

}

You can find Reachability class .h and .m here. Just add the both files in your project. https://github.com/tonymillion/Reachability

If you are in ARC on mode, may be you have to set the fno flags also for the .m

You can also add a reachability observer somewhere (i.e. in viewDidLoad):

Reachability *reachabilityInfo;
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(myReachabilityDidChangedMethod)
                                         name:kReachabilityChangedNotification
                                       object:reachabilityInfo];
Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
1

YOU MUST READ apple's documentation and source available on https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

And in Reachability/APLViewController.m you will find all your answers i.e how to observe for network change notifications and how to update you button state(enable/disable) or even you can update your userinterface

aroragourav
  • 304
  • 3
  • 4