1

我正在使用 ashleymills 的可达性:https ://github.com/ashleymills/Reachability.swift/releases

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

1 回答 1

0

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

在我看来,您不必检查它是 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,请记住,当它可以访问时您必须刷新或更新。

于 2015-10-04T17:43:47.180 回答