Determine network type in iOS 14 Swift 5 without Thirdparty Library

1.3k views Asked by At

CTTelephonyNetworkInfo.currentRadioAccessTechnology is deprecated in iOS 12. so is there any api to get the network type?. we can use NWPathMonitor to know wifi, cellular, ethernet type but not 3g,4g,5g etc. Is there any complete solution for this.

2

There are 2 answers

0
Gordon Dove On

CTTelephonyNetworkInfo.currentRadioAccessTechnology was deprecated in iOS 12, but only because iOS 12 introduced multisim support; hence there is no single radio access technology.

The same information is available in the dictionary CTTelephonyNetworkInfo.serviceCurrentRadioAccessTechnology.

In most cases, you can just use CTTelephonyNetworkInfo.serviceCurrentRadioAccessTechnology[CTTelephonyNetworkInfo.dataServiceIdentifier]

0
SreekanthI On
enum Interface {
    /// A virtual or otherwise unknown interface type
    case other
    
    /// A Wi-Fi link
    case wifi
    
    /// A Cellular link
    case cellular(CellularType)
    
    /// A Wired Ethernet link
    case wiredEthernet
    
    enum CellularType: String, GaugeNetworkType {
        /// 2G network type
        case twoG = "2G"
        
        /// 3G network type
        case threeG = "3G"
        
        /// 4G network type
        case fourG = "4G"
        
        /// 5G network type
        case fiveG = "5G"
        
        // unknown network type
        case unKnown
        
        var value: String {
            self.rawValue
        }
    }
    
    var value: String {
        switch self {
        case .wifi:
            return "Wifi"
        case .wiredEthernet:
            return "WiredEthernet"
        case .cellular(let type):
            return type.rawValue
        case .other:
            return "Other"
        }
    }
}    


    func interface(with path: NWPath) -> Interface {
            if path.usesInterfaceType(.wifi) {
                return .wifi
            } else if path.usesInterfaceType(.cellular) {
                return getCellularType(telephonyInfoData)
            } else if path.usesInterfaceType(.wiredEthernet) {
                return .wiredEthernet
            } else {
                return .other
            }
        }
        

     func getCellularType(_ infoData: CTTelephonyNetworkInfoProtocol) -> Interface {
        guard let carrierTypeName = infoData.cellularType else {
            return .other
        }
        
        logger.debug(withMessage: "carrierTypeName.. \(carrierTypeName)", category: LogCategory.application)
        
        if #available(iOS 14.1, *) {
            switch carrierTypeName {
            case CTRadioAccessTechnologyGPRS,
                 CTRadioAccessTechnologyEdge,
                 CTRadioAccessTechnologyCDMA1x:
                return .cellular(.twoG)
            case CTRadioAccessTechnologyWCDMA,
                 CTRadioAccessTechnologyHSDPA,
                 CTRadioAccessTechnologyHSUPA,
                 CTRadioAccessTechnologyCDMAEVDORev0,
                 CTRadioAccessTechnologyCDMAEVDORevA,
                 CTRadioAccessTechnologyCDMAEVDORevB,
                 CTRadioAccessTechnologyeHRPD:
                return .cellular(.threeG)
            case CTRadioAccessTechnologyLTE:
                return .cellular(.fourG)
            case CTRadioAccessTechnologyNRNSA,
                 CTRadioAccessTechnologyNR:
                return .cellular(.fiveG)
            default:
                return .cellular(.unKnown)
            }
        } else {
            switch carrierTypeName {
            case CTRadioAccessTechnologyGPRS,
                 CTRadioAccessTechnologyEdge,
                 CTRadioAccessTechnologyCDMA1x:
                return .cellular(.twoG)
            case CTRadioAccessTechnologyWCDMA,
                 CTRadioAccessTechnologyHSDPA,
                 CTRadioAccessTechnologyHSUPA,
                 CTRadioAccessTechnologyCDMAEVDORev0,
                 CTRadioAccessTechnologyCDMAEVDORevA,
                 CTRadioAccessTechnologyCDMAEVDORevB,
                 CTRadioAccessTechnologyeHRPD:
                return .cellular(.threeG)
            case CTRadioAccessTechnologyLTE:
                return .cellular(.fourG)
            default:
                return .cellular(.unKnown)
            }
        }
    }