Search code examples
iosios6reachability

"Handle No Internet" for whole app


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.


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.