简体   繁体   中英

Check for active internet connection throughout the app in ios using Reachability class

I'm building an app where its required to sync offline data with server whenever internet connection is active. So currently if internet connection is lost in between while pushing data to server it will get saved in the database and whenever the connection is active it will push data to the server. I'm using new reachability class Version: 3.5 from apple. As per their example for a particular view controller I can do like this

- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];
    [self updateInterfaceWithReachability:self.internetReachability];

}

/*!
 * Called by Reachability whenever status changes.
 */
- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
}


- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
   if (reachability == self.internetReachability)
    {
     //Internet is active again- call api to push data to server
     }
}

This would work for particular view controller. Is there any other method in new Reachability class where it could check this for the whole app run? Or will I have to do this check in every viewcontroller to check for active internet connection?

You can check it by appdelegate. I have done like this before.

@property (strong,nonatomic)Reachability *reach;
@property(nonatomic)NetworkStatus netStatus;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(checkNetworkStatus:)
                                                 name:kReachabilityChangedNotification object:nil];
    reach = [Reachability reachabilityForInternetConnection];
    [reach startNotifier];

    [self checkNetworkStatus:nil];
}

- (void)checkNetworkStatus:(NSNotification *)notice
{
    netStatus = [reach currentReachabilityStatus];
    if (netStatus == NotReachable)
    {
          NSLog(@"The internet is down.");
         // do stuff when network gone. 
    }
    else
    {
          NSLog(@"The internet is working!");
          // do stuff when internet comes active 
          [[NSNotificationCenter defaultCenter] postNotificationName:@"INTERNET_AVAILABLE" object:nil];
    }
}

Now when internet goes and comes it notifies. Add observer for notification in your all view where you require to check internet connectivity. It is working to check internet throughout the app. And properties are synthesised.

======== EDIT

in app delegate.h

+ (BOOL)isActiveInternet;

and in app delegate.m

+ (BOOL)isActiveInternet
    {
        netStatus = [reach currentReachabilityStatus];
        if (netStatus == NotReachable)
        {
              NSLog(@"The internet is down.");
             // do stuff when network gone. 
              return FALSE;
        }
        else
        {
              NSLog(@"The internet is working!");
              // do stuff when internet comes active 
              [[NSNotificationCenter defaultCenter] postNotificationName:@"INTERNET_AVAILABLE" object:nil];
              return TRUE;
        }
    }

so that you can directly call this method anywhere in your project like

if([appdelegate isActiveInternet]) {
      //yes net available do your stuff
}

Pushing data to server do it in app delegate rather than in a particular view controller . That way your reachibily code will be in Appdelegate and u can simply call a method of Appdelegate from a view controller To save data or push to server .

If you don;t want to use Appdelegate use a singleton class for this purpose

The project I worked on with others uses a ReachabilityManager which makes use of singleton pattern, as described Reachability

You can make a subclass of UIViewController which add observation of notifications in viewdidload, and all view controller subclass this viewcontroller.

One approach will be to create a parent UIViewController class for that will handle reachability changes, parent class will be inherited by the rest of the viewController classes.

Another approach will be to create a class that handle reachability is instantiated when the app starts and inform by notifications or delegate or blocks the viewControllers, communication and database handlers that are interested by reachability.

You can also inform the user that is offline by adding subview directly to UIApplication's keyWindow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM