在使用应用程序期间丢失 Reachability 连接时弹出警报 (IOS xcode swift)

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

我是 IOS 应用程序开发的初学者,我想 "popup alert when Reachability connection is lost during using the app (IOS xcode swift)", 但我只在启动我的应用程序时收到弹出警报。当互联网连接丢失时,在使用我的应用程序期间没有弹出警报。请各位帮忙,谢谢!

我做了什么: 1) 创建一个 Reachability.swift 文件并写

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) 编辑 ViewController.swift 文件如下

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

}

试试这个 Reachabilty class,将其添加到您的项目中,然后在您的 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()