How to check 3G,4G and wifi internet connection in swift 2.2

若如初见. 提交于 2021-01-28 09:50:51

问题


import Foundation import SystemConfiguration

public class Reachability {

class func isConnectedToNetwork() -> Bool {

    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
        SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))
    }

    var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
    if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
        return false
    }

    let isReachable = flags == .Reachable
    let needsConnection = flags == .ConnectionRequired

    return isReachable && !needsConnection

}

}

I am using the above written code for checking internet connection for my app, this checks only 3G and WIFI connections. But I need to check for 4G Connections also. Can anyone help me to find the solution.


回答1:


Here is the Reachability from Apple, you need to download and drag Reachability.h/.m to your project.

Then import CoreTelephony and try below.

    if let reachability = Reachability.forInternetConnection() {
        reachability.startNotifier()
        let status = reachability.currentReachabilityStatus()
        if status == .init(0) {
            // .NotReachable
            print("Not Reachable")
        }
        else if status == .init(1) {
            // .ReachableViaWiFi
            print("Reachable Via WiFi")

        }
        else if status == .init(2) {
            // .ReachableViaWWAN
            let netInfo = CTTelephonyNetworkInfo()
            if let cRAT = netInfo.currentRadioAccessTechnology  {
                switch cRAT {
                case CTRadioAccessTechnologyGPRS,
                     CTRadioAccessTechnologyEdge,
                     CTRadioAccessTechnologyCDMA1x:
                    print("Reachable Via 2G")
                case CTRadioAccessTechnologyWCDMA,
                     CTRadioAccessTechnologyHSDPA,
                     CTRadioAccessTechnologyHSUPA,
                     CTRadioAccessTechnologyCDMAEVDORev0,
                     CTRadioAccessTechnologyCDMAEVDORevA,
                     CTRadioAccessTechnologyCDMAEVDORevB,
                     CTRadioAccessTechnologyeHRPD:
                    print("Reachable Via 3G")
                case CTRadioAccessTechnologyLTE:
                    print("Reachable Via 4G")
                default:
                    fatalError("error")
                }
            }
        }
    }



回答2:


Latest reachability swift working code.

if let status = appReachablity?.currentReachabilityStatus {
            var message = ""
            if status == .notReachable {
                // .NotReachable
                message = "DOT##=> Not Reachable"
                print(message)

            }
            else if status == .reachableViaWiFi {
                // .ReachableViaWiFi
                message = "DOT##=> Reachable Via WiFi"
                print(message)
            }
            else if status == .reachableViaWWAN {
                // .ReachableViaWWAN
                let netInfo = CTTelephonyNetworkInfo()
                if let cRAT = netInfo.currentRadioAccessTechnology  {
                    switch cRAT {
                    case CTRadioAccessTechnologyGPRS,
                         CTRadioAccessTechnologyEdge,
                         CTRadioAccessTechnologyCDMA1x:
                        message = "DOT##=> Reachable Via 2G"
                        print(message)
                    case CTRadioAccessTechnologyWCDMA,
                         CTRadioAccessTechnologyHSDPA,
                         CTRadioAccessTechnologyHSUPA,
                         CTRadioAccessTechnologyCDMAEVDORev0,
                         CTRadioAccessTechnologyCDMAEVDORevA,
                         CTRadioAccessTechnologyCDMAEVDORevB,
                         CTRadioAccessTechnologyeHRPD:
                        message = "DOT##=> Reachable Via 3G"
                        print(message)
                    case CTRadioAccessTechnologyLTE:
                        message = "DOT##=> Reachable Via 4G"
                        print(message)
                    default:
                        fatalError("error")
                    }
                }
            }
            Toast(text: message, duration: Delay.long).show()
        }


来源:https://stackoverflow.com/questions/44645390/how-to-check-3g-4g-and-wifi-internet-connection-in-swift-2-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!