简体   繁体   English

显示可达状态警报

[英]Display an alert of reachability status

I'm using Reachability of ashleymills: https://github.com/ashleymills/Reachability.swift/releases 我正在使用ashleymills的可达性: https//github.com/ashleymills/Reachability.swift/releases

In Xcode 7, I wrote a function to display an alert of reachability status. 在Xcode 7中,我编写了一个功能来显示可达性状态警报。

However, the alert never shows up. 但是,警报永远不会显示。

Here is my code: 这是我的代码:

let reachability = Reachability.reachabilityForInternetConnection()
reachability!.whenReachable = { reachability in
    if reachability.isReachableViaWiFi() {
        let alertController = UIAlertController(title: "Alert", message: "Reachable via WiFi", preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

        alertController.addAction(defaultAction)

        self.presentViewController(alertController, animated: true, completion: nil)
    }
    else {
        let alertController = UIAlertController(title: "Alert", message: "Reachable via Cellular", preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

        alertController.addAction(defaultAction)

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

reachability!.whenUnreachable = { reachability in
    let alertController = UIAlertController(title: "Alert", message: "Please connect to internet", preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

    alertController.addAction(defaultAction)

    self.presentViewController(alertController, animated: true, completion: nil)
}

reachability!.startNotifier()

Please replace your code with this and it should work, there might be a bug in Reachability after swift 1.2, so here I'm just checking if its reachable or not and i think its enough. 请更换与此代码,它应该工作,有可能是在一个错误Reachability迅速1.2后,所以在这里如果到达与否,我只是检查,我认为它不够。

In my opinion you don't have to check if its the wifi or cellular, The reason behind the alert isn't showing because its not entering the blocks to show the alert : 在我看来,您不必检查它是wifi还是蜂窝式的。警报背后的原因没有显示,因为它没有进入显示警报的区域:

let useClosures = false

class ViewController: UIViewController {

    let reachability = Reachability.reachabilityForInternetConnection()

    override func viewDidLoad() {
        super.viewDidLoad()

        if (useClosures) {
            reachability?.whenReachable = { reachability in
                print("Reachable")
            }
            reachability?.whenUnreachable = { reachability in
                print("Unreachable")
            }
        } else {
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
        }

        reachability?.startNotifier()

        // Initial reachability check when the app starts
        if let reachability = reachability {
              dispatch_async(dispatch_get_main_queue()) {
            if reachability.isReachable() {
                let alertController = UIAlertController(title: "Alert", message: "Reachable", preferredStyle: .Alert)
                let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

                alertController.addAction(defaultAction)

                self.presentViewController(alertController, animated: true, completion: nil)
            } else {
                let alertController = UIAlertController(title: "Alert", message: "Please connect to internet", preferredStyle: .Alert)
                let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

                alertController.addAction(defaultAction)

                self.presentViewController(alertController, animated: true, completion: nil)
            }
            }
        }
    }

    deinit {

        reachability?.stopNotifier()

        if (!useClosures) {
            NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
        }
    }


    func reachabilityChanged(note: NSNotification) {
        let reachability = note.object as! Reachability
        // Initial reachability check while surfing in the app
        if reachability.isReachable() {
            let alertController = UIAlertController(title: "Alert", message: "Reachable", preferredStyle: .Alert)
            let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

            alertController.addAction(defaultAction)

            self.presentViewController(alertController, animated: true, completion: nil)

        } else {
            let alertController = UIAlertController(title: "Alert", message: "Please connect to internet", preferredStyle: .Alert)
            let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

            alertController.addAction(defaultAction)

            self.presentViewController(alertController, animated: true, completion: nil)
        }
    }
}

Note : You said you have webView in your app, remember that you have to refresh or update when its reachable. 注意:您说过您的应用程序中有webView,请记住您必须刷新或更新它。

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

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