zl程序教程

您现在的位置是:首页 >  硬件

当前栏目

Swift - 判端网络连接状态,连接类型(3G还是Wifi)

WiFi状态连接 类型 还是 swift 网络连接 3G
2023-09-11 14:17:59 时间

IJReachability是一个使用Swift写的第三方网络检测类。可以测试网络是否连接,并支持3G和Wifi的检测。

使用样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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) {
        //判断连接状态
        if IJReachability.isConnectedToNetwork(){
            statusLabel.text = "网络连接:可用"
        }else{
            statusLabel.text = "网络连接:不可用"
        }
         
        //判断连接类型
        let statusType = IJReachability.isConnectedToNetworkOfType()
        switch statusType{
            case .WWAN:
                typeLabel.text = "连接类型:移动网络"
            case .WiFi:
                typeLabel.text = "连接类型:WiFi"
            case .NotConnected:
                typeLabel.text = "连接类型:没有网络连接"
        }
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}


--- IJReachability.swift ---

1
ddd