原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_778.html
1,Reachability.swift 是一个使用Swift写的第三方网络检测类。
可以测试网络是否连接,并支持3G和Wifi的检测。
使用样例:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func checkConnect(sender: AnyObject) {
let reachability = Reachability.reachabilityForInternetConnection()
//判断连接状态
if reachability!.isReachable(){
statusLabel.text = "网络连接:可用"
}else{
statusLabel.text = "网络连接:不可用"
}
//判断连接类型
if reachability!.isReachableViaWiFi() {
typeLabel.text = "连接类型:WiFi"
}else if reachability!.isReachableViaWWAN() {
typeLabel.text = "连接类型:移动网络"
}else {
typeLabel.text = "连接类型:没有网络连接"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
源码下载:hangge_778.zip
2,Reachability.swift 还能监听网络变化,当网络切换的时候会发出通知
具体代码可以访问它的主页:Reachability.swift