0

I want to block my app's ui when there is no network connectivity.

How can I do this?

Creating a blocking wide-as-screen transparent view

move it to the front when needed to block ui touches?

move it to the rear when network is back?

Is there a best UX practice for this backed up in swift implementation?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 1
    well it's probably not a good UI solution from a user point of view... They'll have no idea why your app isn't working. I'd create an alert (either through a custom view or `UIAlertController`) that you can add over your view, that clearly says there's no network connectivity. – Hamish Jan 29 '16 at 14:05
  • I think I'll add a hidden view and unhide it for 2 seconds when network is not available. But how would you trigger the hidding again ? by task? – Elad Benda Jan 29 '16 at 14:08
  • I'm not sure I understand what you mean... you'll want to hide the view again when your app detects an internet connection (see http://stackoverflow.com/questions/8812459/easiest-way-to-detect-internet-connection-on-ios). If you really only want to unhide then hide after 2 seconds, you can use a `dispatch_after` or `performSelector:withObject:afterDelay:`. – Hamish Jan 29 '16 at 14:16

3 Answers3

2

Instead of doing all that you can just disable user interaction for that particular view. Like below

[self.view setUserInteractionEnabled:NO];
self.view.userInteractionEnabled = false //swift implementation

This will disable user interaction for all subviews of that view

Vishnu gondlekar
  • 3,896
  • 21
  • 35
2

If code that handles internet connection is not in currently shown view controller

UIApplication.sharedApplication().keyWindow.rootViewController.view.userInteractionEnabled = false
Volodymyr
  • 1,165
  • 5
  • 16
1

It's not ideal but I have solution based on Nanayakkara project. AppDelegate creates MyConnectionManager which is observed on networkStatusChanged selector:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("networkStatusChanged:"), name: ReachabilityStatusChangedNotification, object: nil)
Reach().monitorReachabilityChanges()

Each time connection state was changed manager calls networkStatusChanged and checks if connection is lost & top view isn't special connection view with message like "Please check your internet connection". If it isn't manager retrieves topController from sharedApplication

func topController() -> UIViewController? {
    if var topRootController =
        UIApplication.sharedApplication().keyWindow?.rootViewController {
            while((topRootController.presentedViewController) != nil) {
                topRootController = topRootController.presentedViewController!
            }
            return topRootController
        }
        return nil
    }

and calls presentViewController with ConnectionViewController.

Leo
  • 3,003
  • 5
  • 38
  • 61