0

I am developing an iOS app that needs to check if Internet connection is available or not in it's several ViewController classes. To check network connectivity , I am using (Reachability library by Tony Million).

Here is what I am doing right now. In a ViewController class that requires connectivity checking , I am using this code blocks at ViewDidLoad....

Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability*reach)
{
    NSLog(@"REACHABLE!"); // Load offline data from core data
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!"); // Fetch data from Rest Api
};

[reach startNotifier];

But I am having some unexpected behaviour when network status changes and my app is in this ViewController, like sometimes fetching data from api runs like a loop again and again. I'm not sure if I have done something wrong in my codes, so expecting comments from experts.

Anyway, I am thinking an alternative solution to solve this issue and make codes cleaner and stable. I want to know from experts if it's good approach. I want to keep a BOOL Variable in AppDelegate called isNetworkConnected . Then in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions , I want to run the above codes and set the variable isNetworkConnected YES/NO in that. So, the code will be like this...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
    reach.reachableBlock = ^(Reachability*reach)
    {
        NSLog(@"REACHABLE!"); 
         isNetworkConnected = YES ;
    };

    reach.unreachableBlock = ^(Reachability*reach)
    {
        NSLog(@"UNREACHABLE!"); 
        isNetworkConnected = NO ;
    };

    [reach startNotifier];

    // rest codes....
} 

Then , in my ViewController , I'll do like this...

if(appdelegate.isNetworkConnected == YES){
    // Fetch data from api
} else{
   // Load offline data from Core data
}

That's it. I want to know whether my idea is good or bad , if I am doing any mistake or any better suggestion will be highly appreciated. Thanks in advance.

ayon
  • 2,150
  • 2
  • 16
  • 31
  • 1
    Check out this answer, You can check reachability in all the viewControllers. http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk – nsgulliver Oct 02 '13 at 11:16
  • Thanks , I have already seen it before. And I am using the solution above from the most voted answer there. I am just trying to make the connectivity variable Gloabal in the app so that I don't need to use the whole blocks again and again in every ViewController. – ayon Oct 02 '13 at 11:28

2 Answers2

1

Try my code below.

In your AppDelegate.h make a method like below.

-(void)checkNetConnection {

    bool success = false;
    const char *host_name = [@"http://stackoverflow.com"
                             cStringUsingEncoding:NSASCIIStringEncoding];

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
    SCNetworkReachabilityFlags flags;
    success = SCNetworkReachabilityGetFlags(reachability, &flags);
    bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
    !(flags & kSCNetworkFlagsConnectionRequired);

    [[NSUserDefaults standardUserDefaults]setBool:isAvailable forKey:@"ISNETAVAILABLE"];
}

Call this method from application didFinishLaunchingWithOptions:

[self checkNetConnection];

In your any other viewcontroller where you want to check just simple define a Bool variable like below.

bool isAvailable = [[NSUserDefaults standardUserDefaults]boolForKey:@"ISNETAVAILABLE"];

if (isAvailable) {

    // do what you want to do if internet is available.

}

else {

    // internet is not available.

}

Hope this helps you.

thomers
  • 2,483
  • 4
  • 26
  • 47
Manthan
  • 3,842
  • 1
  • 25
  • 56
0

Define 1 Bool varible in AppDelegate.h file and check this Bool throughout the class. check if the net connection is on Bool Make true. and if internet connection is unreachable bool is false.

and themn check that bool wher you want.

Jitendra
  • 4,944
  • 2
  • 20
  • 42
  • This is exactly the solution I described above in my question. Please kindly check my codes and let me know if I am doing anything wrong. Thanks for your answer. – ayon Oct 02 '13 at 11:40