在使用应用程序期间失去可达性连接时弹出警报(IOS xcode swift) [英] popup alert when Reachability connection is lost during using the app (IOS xcode swift)

查看:107
本文介绍了在使用应用程序期间失去可达性连接时弹出警报(IOS xcode swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是IOS应用程序开发的初学者,想在使用应用程序期间失去可达性连接(IOS xcode swift)时弹出警报,
,但我仅在启动我的应用程序时收到弹出警报。 Internet连接断开时,在使用我的应用程序期间没有警报弹出窗口。

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!

我所做的:
1)创建一个Reachability.swift文件并编写

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

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

}


推荐答案

试试这个反应堆habilty 类,将其添加到您的项目中,然后在 viewController

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

这篇关于在使用应用程序期间失去可达性连接时弹出警报(IOS xcode swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆