检测 Internet 连接并显示 UIAlertview Swift 3 [英] Detect Internet Connection and display UIAlertview Swift 3

查看:26
本文介绍了检测 Internet 连接并显示 UIAlertview Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,通过使用 if else 语句来检测是否有互联网连接,当有互联网时,什么都不做,但如果没有互联网连接,然后提醒视图说该应用程序需要我管理的互联网找到了可达性,但要在我的 viewDidLoad() 上实现,uialertview 似乎不起作用.我正在使用这个:

I am making an app that detects if there is a connection to internet by using if else statement, when there is internet, do nothing but if there are no internet connection, then alert view to say the app requires an internet I managed found Reachability but to implement on my viewDidLoad() the uialertview seems to be not working. I am using this:

public class Reachability {
class func isConnectedToNetwork() -> Bool {
    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
        $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
            SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
        }
    }
    var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
    if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
        return false
    }
    let isReachable = flags == .reachable
    let needsConnection = flags == .connectionRequired
    return isReachable && !needsConnection
    }
}

然后在我的视图控制器类中:

then in my View Controller class:

override func viewDidLoad() {
        if Reachability.isConnectedToNetwork() == true
        {
            print("Connected")
        }
        else
        {
            let controller = UIAlertController(title: "No Internet Detected", message: "This app requires an Internet connection", preferredStyle: .alert)
            let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
            let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

            controller.addAction(ok)
            controller.addAction(cancel)

            present(controller, animated: true, completion: nil)
        }
        super.viewDidLoad()
        //...
    }

看起来没有弹出警报视图有什么建议吗?或帮助?谢谢

and looks like the alertview doesn't popup any suggestion? or help? thanks

推荐答案

您必须将 AlertController 放在 ViewControllers 生命周期的 viewDidAppear 方法中,因为在方法 viewDidLoad 和 viewWillAppear 中,您当前的视图仍然不在窗口层次结构中.
如果你想呈现更多的 ViewControllers,比如 AlertController,你需要一个完整加载的视图,你的新 ViewController 可以在其中呈现:

You have to put the AlertController in the viewDidAppear method of your ViewControllers lifecycle, because in the methods viewDidLoad und viewWillAppear your current view is still not in the window hierachy.
If you want to present further ViewControllers like the AlertController you need a full loaded view in which your new ViewController could be rendered:

override func viewDidLoad() {
    super.viewDidLoad()
    //ViewControllers view not in the window hierarchy
    // Here you could make further initialization of your views subviews
}

override func viewWillAppear(_ animated: Bool) {
    //ViewControllers view ist still not in the window hierarchy
    //This is the right place to do for instance animations on your views subviews
}

override func viewDidAppear(_ animated: Bool) {
    // ViewControllers view ist fully loaded and could present further ViewController
    //Here you could do any other UI operations
    if Reachability.isConnectedToNetwork() == true
    {
        print("Connected")
    }
    else
    {
        let controller = UIAlertController(title: "No Internet Detected", message: "This app requires an Internet connection", preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

        controller.addAction(ok)
        controller.addAction(cancel)

        present(controller, animated: true, completion: nil)
    }

}

这是 Apple 文档中关于 ViewController 的内容:UIViewController 方法被调用如下:

This is what the Apple documentation is saying about ViewController: UIViewController methods get called as follows:

  • viewDidLoad()——当视图控制器的内容视图(顶部其视图层次结构)是从故事板创建和加载的.这个方法用于初始设置.但是,由于视图可能由于应用程序中的资源有限而被清除,无法保证它只会被调用一次.
  • viewWillAppear()——适用于任何您希望始终在视图变为之前发生的操作可见的.因为视图的可见性可能会被切换或遮挡其他视图,此方法总是在紧接在内容视图出现在屏幕上.
  • viewDidAppear()——适用于任何您希望在视图变为可见,例如获取数据或显示动画.因为一个视图的可见性可能会被其他视图切换或遮挡,这方法总是在内容视图出现后立即调用屏幕上.

参考:https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html

这篇关于检测 Internet 连接并显示 UIAlertview Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆