ios - 在使用应用程序期间丢失可达性连接时弹出警报(IOS xcode swift)

标签 ios swift alert reachability

我是 IOS 应用程序开发的初学者,我想“在使用该应用程序(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类,将其添加到您的项目中,并在您的 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()

关于ios - 在使用应用程序期间丢失可达性连接时弹出警报(IOS xcode swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400192/

相关文章:

ios - 错误 "a valid provisioning file for this executable was not found"

ios - 如何使用不接受实时输入的 CoreML 在 Swift 中创建一个简单的相机应用程序?

xcode - 从 UITableViewCell 调用 View Controller 中的函数

css - 由于屏幕英寸改变页面样式

ios - 是否可以让客户从您的 iOS 应用程序中购买 iTunes 的流媒体内容?

ios - 在 Swift iOS 中未调用 Google Places 回调

java - Selenium 驱动程序中的随机警报,我该如何处理?

javascript - 如果用户使用 onbefoerunload 导航,如何在 jexcel 单元格发生更改时发出警报

ios - Swift .insert 在数据库条目中

ios - 如何使用 NSURLSession 快速发送 HTTPS POST 请求