Question

I am developing an app for iOS6.0 and >. It is tab-based navigation controller app

I have say 10 UIViewControllers and each viewcontroller requires internet to work.

So now my Question is What is the best way to handle No Internet stuff? Also if Internet Connection is available then the app must again work as normal.

P.S. I am totally aware of Reachability class. But I don't want to set the reachability changed notification in all the 10 View Controllers.

There must be some way to do this means In whatever view controller I am, It will show No Internet View and When internet is back It works as usual. Something like Present No Internet View When Internet is not there or similar. But not sure How?

Not sure though But I have heard that Apple has provided something which Displays "No Internet" Message on Top of the app and does not allows user to navigate unless Internet is back.

I need exactly the same.

Any guide towards success will be really appreciated.

Was it helpful?

Solution

I would choose a little different approach. Why not create a UIViewController Subclass that handles the internet connection notifications for you? You could do something like this half-pseudo code. I just wrote this code out of my head. It may contain errors. So don't just copy & paste.

@interface SMInternetBaseViewController : UIViewController {
    SMOverlay* overlay;
}
@end

@interface SMInternetBaseViewController()
- (void)reachabilityChanged:(NSNotification *) not;
@end

@implementation SMInternetBaseViewController
- (id)init {
    self = [super init];
    if (self) {
        // Register here the method reachabilityChanged for Reachability notifications 
    }
    return self;
}

- (void)reachabilityChanged:(NSNotification *) not
{
    // Define here how to behave for different notifications

    if (__NO_INTERNET__) {
        // Add an overlay to the view.
        if (!overlay) {
            overlay = [[SMOverlay alloc] init];
        }
        [self.view addSubview:overlay];
    }

    if (__AGAIN_INTERNET__) {
        [overlay removeFromSuperview];
    }

}
@end

Then you could easily make all your view controllers subclass of this SMInternetBaseViewController and you don't have to care about it anymore.

OTHER TIPS

You don't need to create notifier you can check network connection using reachability class as

Reachability *networkReachability = [Reachability reachabilityForInternetConnection];   
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];    

if (networkStatus == NotReachable)
{
  //Notify that internet is not available

}

You can use this Control SMBInternetConnectionIndicator

Your phone might still have internet, but your server or domain may be down, to handle this correctly you use the following with your domain name to ping.

#import <SystemConfiguration/SystemConfiguration.h>

if ([self gotConnection]) //do what you want
else //warn that your server is down

- (BOOL)gotConnection
{
    static BOOL checkNetwork = YES;
    BOOL check;
    if (checkNetwork) {
        checkNetwork = NO;

        Boolean success;    
        const char *host_name = "<#yourdomain.com#>";

        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
        SCNetworkReachabilityFlags flags;
        success = SCNetworkReachabilityGetFlags(reachability, &flags);
        check = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
        CFRelease(reachability);
    }
    return check;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top