import Foundation import CoreTelephony /// 设备网络类型 public enum NetworkType { case unknow, unconnect, wifi /// 手机网络 case phone(NetPhoneType) /// 当前的连接网络类型 var type: String { switch self { case .unknow: return "unknow" case .unconnect: return "unconnect" case .wifi: return "wifi" case .phone(let netPhoneType): return netPhoneType.rawValue } } /// 手机网络类型 public enum NetPhoneType: String { case unknow = "unknow" case _2G = "2G" case _3G = "3G" case _4G = "4G" case _5G = "5G" static var type: NetPhoneType { return getNetPhoneType() } } } private extension NetworkType.NetPhoneType { /// 获取手机网络类型 static func getNetPhoneType() -> NetworkType.NetPhoneType { let info = CTTelephonyNetworkInfo() var type: String if #available(iOS 12, *) { type = info.serviceCurrentRadioAccessTechnology?.values.first ?? "" } else { type = info.currentRadioAccessTechnology ?? "" } guard !type.isEmpty else { return .unknow } let _2gTypes = [CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyCDMA1x] if _2gTypes.contains(type) { return ._2G } let _3gTypes = [CTRadioAccessTechnologyHSDPA, CTRadioAccessTechnologyWCDMA, CTRadioAccessTechnologyHSUPA, CTRadioAccessTechnologyCDMAEVDORev0, CTRadioAccessTechnologyCDMAEVDORevA, CTRadioAccessTechnologyCDMAEVDORevB, CTRadioAccessTechnologyeHRPD] if _3gTypes.contains(type) { return ._3G } let _4gTypes = [CTRadioAccessTechnologyLTE] if _4gTypes.contains(type) { return ._4G } if #available(iOS 14.1, *) { let _5gTypes = [CTRadioAccessTechnologyNR, CTRadioAccessTechnologyNRNSA] if _5gTypes.contains(type) { return ._5G } } return .unknow } }