Search code examples
iosswiftuialertcontrollerreachabilityreachability-swift

Display an alert of reachability status


I'm using Reachability of ashleymills: https://github.com/ashleymills/Reachability.swift/releases

In Xcode 7, I wrote a function to display an alert of reachability status.

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

Solution

  • 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.

    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 :

    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.