Ios 显示可达性状态警报

Ios 显示可达性状态警报,ios,swift,uialertcontroller,reachability,reachability-swift,Ios,Swift,Uialertcontroller,Reachability,Reachability Swift,我正在使用ashleymills的可达性: 在Xcode 7中,我编写了一个函数来显示可达性状态的警报 然而,警报从未出现 这是我的密码: let reachability = Reachability.reachabilityForInternetConnection() reachability!.whenReachable = { reachability in if reachability.isReachableViaWiFi() { let alertCont

我正在使用ashleymills的可达性:

在Xcode 7中,我编写了一个函数来显示可达性状态的警报

然而,警报从未出现

这是我的密码:

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()

请用这个替换你的代码,它应该可以工作,在swift 1.2之后,
Reachability
中可能有一个bug,所以这里我只是检查它是否可以访问,我认为这已经足够了

在我看来,您不必检查是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)
        }
    }
}

注意:你说你的应用程序中有webView,请记住,当它可以访问时,你必须刷新或更新它。

请用这个替换你的代码,它应该可以工作,在swift 1.2之后,
可访问性中可能有一个bug,所以我在这里只是检查它是否可以访问,我认为这就足够了

在我看来,您不必检查是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)
        }
    }
}

注意:您说过您的应用程序中有webView,请记住,您必须在其可访问时进行刷新或更新。

谢谢!只需测试您的代码,但警报永远不会出现。@GPH,您将此代码放在哪里?在
视图中加载
?顺便说一句,在我的情况下,它显示!是的,在ViewController.swifit的viewDidLoad功能中,它仅在启动应用程序时显示,如果启动应用程序后互联网连接发生变化,则不会显示警报。顺便说一句,添加代码后,webview无法显示任何内容。我修复了一些问题,并添加了您的代码。它显示错误如下谢谢!只需测试您的代码,但警报永远不会出现。@GPH,您将此代码放在哪里?在
视图中加载
?顺便说一句,在我的情况下,它显示!是的,在ViewController.swifit的viewDidLoad功能中,它仅在启动应用程序时显示,如果启动应用程序后互联网连接发生变化,则不会显示警报。顺便说一句,添加代码后,webview无法显示任何内容。我修复了一些问题,并添加了您的代码。它显示错误如下:我认为问题可能是没有在主线程上调用闭包,因此将
UIAlertController
代码包装在
dispatch\u async(dispatch\u get\u main\u queue()){…}
中应该可以解决问题,我认为问题可能是没有在主线程上调用闭包,因此,将
UIAlertController
代码包装在
dispatch\u async(dispatch\u get\u main\u queue()){…}
中应该可以解决这个问题