1

CTTelephonyNetworkInfo.currentRadioAccessTechnology在 iOS 12 中已弃用。那么是否有任何 api 来获取网络类型?我们可以使用 NWPathMonitor 知道 wifi、蜂窝、以太网类型,但不知道 3g、4g、5g 等。有没有完整的解决方案。

4

1 回答 1

0
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)
            }
        }
    }
于 2021-11-17T17:44:54.100 回答