How to determine the iOS connection type (Edge, 3G

2019-03-09 04:38发布

问题:

How is it possible to determine the iOS connection type?

I want to know if there is Edge, 3G, 4G oder Wifi available at this moment on my device.

This information I need to load different data with a different size.

So, I don't want to load an image of 4 MB with a connection type "Edge".

回答1:

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 ""
    }


}


回答2:

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";
        }

    }


回答3:

You can use CTTelephonyNetworkInfo class from Apple. You can use this API on iOS 7+

You need to import it : @import CoreTelephony



回答4:

CoreTelephony.framework is needed.

let networkInfo = CTTelephonyNetworkInfo()
let networkString = networkInfo.currentRadioAccessTechnology

if networkString == CTRadioAccessTechnologyLTE{
  // LTE (4G)
}else if networkString == CTRadioAccessTechnologyWCDMA{
  // 3G 
}else if networkString == CTRadioAccessTechnologyEdge{
  // EDGE (2G)
}


回答5:

import CoreTelephony

enum RadioAccessTechnology: String {
    case cdma = "CTRadioAccessTechnologyCDMA1x"
    case edge = "CTRadioAccessTechnologyEdge"
    case gprs = "CTRadioAccessTechnologyGPRS"
    case hrpd = "CTRadioAccessTechnologyeHRPD"
    case hsdpa = "CTRadioAccessTechnologyHSDPA"
    case hsupa = "CTRadioAccessTechnologyHSUPA"
    case lte = "CTRadioAccessTechnologyLTE"
    case rev0 = "CTRadioAccessTechnologyCDMAEVDORev0"
    case revA = "CTRadioAccessTechnologyCDMAEVDORevA"
    case revB = "CTRadioAccessTechnologyCDMAEVDORevB"
    case wcdma = "CTRadioAccessTechnologyWCDMA"

    var description: String {
        switch self {
        case .gprs, .edge, .cdma:
            return "2G"
        case .lte:
            return "4G"
        default:
            return "3G"
        }
    }
}    

// Example:
let networkInfo = CTTelephonyNetworkInfo()
let networkString = networkInfo.currentRadioAccessTechnology
let tecnology = RadioAccessTechnology(rawValue: networkString)

print(tecnology.description)


标签: ios swift 3g edge 4g