How to determine the iOS connection type (Edge, 3G, 4G, Wifi)? [duplicate] How to determine the iOS connection type (Edge, 3G, 4G, Wifi)? [duplicate] ios ios

How to determine the iOS connection type (Edge, 3G, 4G, Wifi)? [duplicate]


For swift we can use following function:

func getNetworkType()->String {    do{        let reachability:Reachability = try Reachability.reachabilityForInternetConnection()        do{            try reachability.startNotifier()            let status = reachability.currentReachabilityStatus            if(status == .NotReachable){                return ""            }else if (status == .ReachableViaWiFi){                return "Wifi"            }else if (status == .ReachableViaWWAN){                let networkInfo = CTTelephonyNetworkInfo()                let carrierType = networkInfo.currentRadioAccessTechnology                switch carrierType{                case CTRadioAccessTechnologyGPRS?,CTRadioAccessTechnologyEdge?,CTRadioAccessTechnologyCDMA1x?: return "2G"                case CTRadioAccessTechnologyWCDMA?,CTRadioAccessTechnologyHSDPA?,CTRadioAccessTechnologyHSUPA?,CTRadioAccessTechnologyCDMAEVDORev0?,CTRadioAccessTechnologyCDMAEVDORevA?,CTRadioAccessTechnologyCDMAEVDORevB?,CTRadioAccessTechnologyeHRPD?: return "3G"                case CTRadioAccessTechnologyLTE?: return "4G"                default: return ""                }            }else{                return ""            }        }catch{            return ""        }    }catch{        return ""    }}


Swift 5, iOS 12.0 or later:

import SystemConfigurationimport CoreTelephonyimport Foundationclass func getConnectionType() -> String {        guard let reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.google.com") else {            return "NO INTERNET"        }        var flags = SCNetworkReachabilityFlags()        SCNetworkReachabilityGetFlags(reachability, &flags)        let isReachable = flags.contains(.reachable)        let isWWAN = flags.contains(.isWWAN)        if isReachable {            if isWWAN {                let networkInfo = CTTelephonyNetworkInfo()                let carrierType = networkInfo.serviceCurrentRadioAccessTechnology                guard let carrierTypeName = carrierType?.first?.value else {                    return "UNKNOWN"                }                switch carrierTypeName {                case CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyCDMA1x:                    return "2G"                case CTRadioAccessTechnologyLTE:                    return "4G"                default:                    return "3G"                }            } else {                return "WIFI"            }        } else {            return "NO INTERNET"        }    }


in Objective C:

first import @import CoreTelephony.

Reachability *reachability = [Reachability reachabilityForInternetConnection];    [reachability startNotifier];    NetworkStatus status = [reachability currentReachabilityStatus];    if(status == NotReachable)    {        //No internet    }    else if (status == ReachableViaWiFi)    {        //WiFi    }    else if (status == ReachableViaWWAN)    {        CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];        NSString * carrierType = netinfo.currentRadioAccessTechnology;        if ([carrierType isEqualToString:CTRadioAccessTechnologyGPRS]) {//             @"2G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyEdge]) {//             @"2G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyWCDMA]) {//             @"3G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSDPA]) {//             @"3G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSUPA]) {//             @"3G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMA1x]) {//             @"2G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {//             @"3G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {//             @"3G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {//             @"3G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyeHRPD]) {//             @"3G";        } else if ([carrierType isEqualToString:CTRadioAccessTechnologyLTE]) {//             @"4G";        }    }


matomo