I'm using AFNetworking in my app, I'm checking for internet connectivity by their inbuilt class. Here's the code for this,
- (void) startInternetConnectivityMonitoring
{
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
{
switch (status) {
case AFNetworkReachabilityStatusUnknown:
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
break;
case AFNetworkReachabilityStatusNotReachable:
break;
default:
break;
}
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
and I can check for it by this function,
- (BOOL) connected {
return [AFNetworkReachabilityManager sharedManager].reachable;
}
I'm also using RESideMenu in my app to have a side menu, this is my - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self startInternetConnectivityMonitoring];
RootViewController *rootView = [[RootViewController alloc] init];
[rootView awakeFromNib];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = rootView;
[self.window makeKeyAndVisible];
return YES;
}
Everything is working fine for all other view controllers, except the FirstViewController. In FirstViewController I'm calling a networking request, before that, I do check for internet connectivity. Here's the problem. Each time, it show me No internet
for FirstViewController. Because, while it call for a network request, AFNetworking
don't update the status of internet. It may be a friction of seconds to update but its not updating that much fast. Since, I'm aware of the issue, I don't want to apply a patch to solve this. I tried different things to solve this. Used,
sleep(2);
before return YES from didFinishLaunchingWithOptions...
dispatch_synq
- Freeze the app
dispatch_asynq
- Doesn't work
.. and many other hacky ways
- Doesn't help yet.
I found a simple patch for this, and yes it should work like a charm while I'll not find a reasonable solution for this problem.
BOOL
in AppDelegate.h
BOOL isCalledOnce;
AppDelegate.m
)- (void) setAppRequestCallOnce { isCalledOnce = YES; }
- (BOOL) checkAppRequestCalledOnce {
return isCalledOnce;
}
- (BOOL) connected { if(![self checkAppRequestCalledOnce]) { [self setAppRequestCallOnce]; return YES; } return [AFNetworkReachabilityManager sharedManager].reachable; }
Conclusion:
From FirstViewController
when I check for the reachability, something like,
if(![delegateObj connected]) {
//show no internet message
}
This will first call checkAppRequestCalledOnce
method, which returns a isCalledOnce
, if its NO
then I'm set it to YES
and return YES
(here we're not sure for actual status of reachability). From now, when I again check for reachability then it'll not check for above condition and will return actual reachability status.
Why I'm using because, even if there'll be no internet available at start, AFNetworking
will give us an error about it though we can show it even if we don't have actual status of reachability.
If you find any more elegant way then please post or comment.