Reputation: 2180
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.
Upvotes: 0
Views: 2242
Reputation: 3914
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.
Upvotes: 1
Reputation: 5081
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.
Upvotes: 0