简体   繁体   中英

How to disable user interaction for all UIViewControllers when user is not connected to the internet?

I know how to check whether a user is connected to the internet: https://stackoverflow.com/a/39782859/9735046
How do I disable user interaction for ALL my UI View Controllers if a user is not connected to the internet?

Well, I have to point out that blocking the whole UI is not a good idea from a UX point of view :)

That said, a generally applied solution for blocking the interaction is to modally show some kind of popup until the operation is complete (or, in your case, reachability is reestablished). This popup could at least show a hint to the user what is going on ("please stand by, no internet connection available" or something like this).

The simplest solution is to just use a UIAlertViewController without buttons; there also are lots of nice components available as CocoaPods.

I have made one screen for that when Internet goes off it will push current navigation stack and when internet connected i will pop that No internet screen and show last Top Viewcontroller of Navigation stack

Just put this code on your Appdelegate didFinish launch method

AFNetworkReachabilityManager.shared().startMonitoring()

    AFNetworkReachabilityManager.shared().setReachabilityStatusChange
        { (status: AFNetworkReachabilityStatus) -> Void in

            if ((status == .notReachable) && ! 
(self.mainNav.topViewController is InternetViewController))
            {
                let internetVC:InternetViewController = storyboard.instantiateViewController(withIdentifier: "InternetViewController") as! InternetViewController

                self.mainNav.pushViewController(internetVC, animated: true)
            }
            else if ((status != .notReachable) && (self.mainNav.topViewController is InternetViewController))
            {
                self.mainNav.popViewController(animated: true)
            }
    }

By this code you can Achieve this kind of result and this will look more Professional and user friendly and user can easily know that internet is goes off if you disable UIView and if user is now aware internet is disconnected he assume that your app is stuck

在此处输入图片说明

Just show NoInternetView with the label "No internet connection" and the button "Try again". If there is a connection, hide the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM