【问题标题】:popup alert when Reachability connection is lost during using the app (IOS xcode swift)在使用应用程序期间失去可达性连接时弹出警报(IOS xcode swift)
【发布时间】:2015-07-14 07:22:24
【问题描述】:

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

}

【问题讨论】:

标签: ios swift alert reachability


【解决方案1】:

试试这个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()

【讨论】:

  • 谢谢,但我真的是初学者,请指导我一步一步如何去做。
  • 1.) 将reachability.swift 文件添加到您的项目中。 2)并将上面的代码粘贴到您的 viewDidLoad 方法中。或 applicationDidFinishLaunchingWithOptions 方法。并运行代码
  • 非常感谢!我按照您的回答,它的工作原理是 99%。我把你的 ViewController 的 viewDidLoad 方法放了。
  • 现在唯一的问题是在没有互联网连接的情况下启动应用程序时没有警报。你能帮忙吗?
  • 而不是 println 放你的警报代码兄弟。并且 UIAlertView 在 iOS8 中被贬低了。使用 UIAlertController
猜你喜欢
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多