您当前的位置:首页>swift频道>Swift之判端网络连接状态,连接类型(3G还是Wifi)

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

发布时间:2015-07-06 类型: swift频道 编辑: 小猪迷

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

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

    使用代码如下:

    //  Created by xiaozhumi  on 15/7/6.
    //  Copyright (c) 2015年 xiaozhumi . All rights reserved.

    import UIKit

    class WifiMobileController: UIViewController {
        
        var availabilityLabel: UILabel!
        
        var connectionTypeLabel: UILabel!
        
        override func viewDidLoad() {
            
            super.viewDidLoad()
            
            var button:UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
            button.frame = CGRectMake(100, 100, 100, 100)
            button.setTitle("点我查看网络", forState: UIControlState.Normal)
            button.addTarget(self, action: "checkButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
            self.view.addSubview(button)
            
            availabilityLabel = UILabel(frame:CGRectMake(self.view.frame.size.width/8,20,self.view.frame.size.width*3/4,100))
            connectionTypeLabel = UILabel(frame:CGRectMake(self.view.frame.size.width/8,60,self.view.frame.size.width*3/4,100))
            
            
            self.view.addSubview(availabilityLabel)
            self.view.addSubview(connectionTypeLabel)
            
        }
        
        func checkButtonTapped(sender: UIButton) {
            
            if IJReachability.isConnectedToNetwork() {
                availabilityLabel.text = "Network Connection: Available"
                availabilityLabel.textColor = UIColor.greenColor()
            } else {
                availabilityLabel.text = "Network Connection: Unavailable"
                availabilityLabel.textColor = UIColor.redColor()
            }
            
            let statusType = IJReachability.isConnectedToNetworkOfType()
            switch statusType {
            case .WWAN:
                connectionTypeLabel.text = "Connection Type: Mobile"
                connectionTypeLabel.textColor = UIColor.yellowColor()
            case .WiFi:
                connectionTypeLabel.text = "Connection Type: WiFi"
                connectionTypeLabel.textColor = UIColor.greenColor()
            case .NotConnected:
                connectionTypeLabel.text = "Connection Type: Not connected to the Internet"
                connectionTypeLabel.textColor = UIColor.redColor()
            }
        }
    }


    IJReachability类下载如下:

    Swift之判端网络连接状态,连接类型(3G还是Wifi)1IJReachability.swift.zip


  • 没有评论哦,快来抢个沙发吧
小猪迷

                

微信好友

用微信扫描二维码,
加我好友。

微信公众号

用微信扫描二维码,
订阅小猪迷公众号。

234113618

用微信扫描二维码,
加入小猪迷 QQ 群。

Swift之判端网络连接状态,连接类型(3G还是Wifi)-小猪迷PHP[xiaozhumi.com]

IJReachability是一个使用Swift写的第三方网络检测类.可以测试网络是否连接,并支持3G和Wifi的检测,如下图:使用代码如下://Createdbyxiaozhumi...

https://www.xiaozhumi.com/swift/18.html