简体   繁体   English

当用户未连接到互联网时,如何禁用所有UIViewController的用户交互?

[英]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 我知道如何检查用户是否连接到互联网: 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? 如果用户未连接到互联网,如何禁用所有UI View Controller的用户交互?

Well, I have to point out that blocking the whole UI is not a good idea from a UX point of view :) 好吧,我必须指出,从UX的角度来看,阻塞整个UI并不是一个好主意:)

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; 最简单的解决方案是只使用不带按钮的UIAlertViewController there also are lots of nice components available as CocoaPods. 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 我已经为此制作了一个屏幕,当Internet断开时它将推送当前的导航堆栈,而当Internet连接时我会弹出该屏幕,并显示最后一个导航堆栈的顶部Viewcontroller

Just put this code on your Appdelegate didFinish launch method 只需将此代码放在您的Appdelegate didFinish启动方法中

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 通过此代码,您可以实现这种结果,并且看起来更加专业和用户友好,如果禁用UIView,并且用户现在知道互联网已断开连接,则用户可以轻松地知道互联网已关闭,他认为您的应用程序已卡死

在此处输入图片说明

Just show NoInternetView with the label "No internet connection" and the button "Try again". 只需显示NoInternetView并显示标签“ No internet connection”和按钮“ Try again”。 If there is a connection, hide the view. 如果存在连接,请隐藏视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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