Search code examples
iosswiftalertreachability

popup alert when Reachability connection is lost during using the app (IOS xcode swift)


I am a beginner of IOS app development and would like to "popup alert when Reachability connection is lost during using the app (IOS xcode swift)", but I only get popup alert when starting the my app. There is not alert popup during using my app when internet connection lost. Please kindly help, thanks!

What I did: 1) creat a Reachability.swift file and worte

import Foundation

public class Reachability {

class func isConnectedToNetwork()->Bool{

    var Status:Bool = false

    let url = NSURL(string: "http://google.com/")

    let request = NSMutableURLRequest(URL: url!)

    request.HTTPMethod = "HEAD"
    request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
    request.timeoutInterval = 10.0        

    var response: NSURLResponse?

    var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?

    if let httpResponse = response as? NSHTTPURLResponse {

        if httpResponse.statusCode == 200 {
            Status = true
        }
    }

    return Status
 }
 }

2) edit the ViewController.swift file as below

import UIKit
class ViewController: UIViewController {

@IBOutlet weak var WebView: UIWebView!

//ViewDidLoad method
override func viewDidLoad() {
    super.viewDidLoad()

    if Reachability.isConnectedToNetwork() == true {
        println("Internet connection OK")
    } else {
        println("Internet connection FAILED")
        var alert = UIAlertView(title: "No Internet Connection",
                                message: "Make sure your device is connected to the internet.", 
                                delegate: nil, 
                                cancelButtonTitle: "OK")

        alert.show()

    }
    var URL = NSURL(string: "http://www.google.com/")

    WebView.loadRequest(NSURLRequest(URL: URL!))
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

Solution

  • Try this Reachabilty class, add it in your project and do the following in your viewController

    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)
    
        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)
    
        presentViewController(alertController, animated: true, completion: nil)
        }
      }
    reachability.whenUnreachable = { reachability in
    let alertController = UIAlertController(title: "Alert", message: "Not Reachable", preferredStyle: .Alert)
    
        let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
    
        presentViewController(alertController, animated: true, completion: nil)
    }
    
    reachability.startNotifier()