iPhone active network type (2G, 3G, WiFi)

后端 未结 3 478
萌比男神i
萌比男神i 2020-12-13 08:10

Does anyone know how to determine the active network type at the specific moment: 2G, 3G or WiFi.

For example, at a specific moment there

3条回答
  •  攒了一身酷
    2020-12-13 08:21

    This is the way to find the network mode(2G,3G,4G or wifi) of your device in swift.

    if let reachability = Reachability.forInternetConnection() {
    
            reachability.startNotifier()
    
            let status = reachability.currentReachabilityStatus()
    
            if status == .init(0) {
                // .NotReachable
    
                print("Not Reachable")
    
            }
            else if status == .init(1) {
                // .ReachableViaWiFi
    
                print("Reachable Via WiFi")
    
            }
            else if status == .init(2) {
                // .ReachableViaWWAN
                let netInfo = CTTelephonyNetworkInfo()
    
                if let cRAT = netInfo.currentRadioAccessTechnology  {
    
                    switch cRAT {
    
                    case CTRadioAccessTechnologyGPRS,
                         CTRadioAccessTechnologyEdge,
                         CTRadioAccessTechnologyCDMA1x:
    
                        print("Reachable Via 2G")
    
    
                        do{
                            try realm.write {
                                realm.add(ModalDataSaver.singletonClass)
                            }
                        }catch
                        {
                            print("Error in saving data :- \(error.localizedDescription)")
                        }
    
    
                    case CTRadioAccessTechnologyWCDMA,
                         CTRadioAccessTechnologyHSDPA,
                         CTRadioAccessTechnologyHSUPA,
                         CTRadioAccessTechnologyCDMAEVDORev0,
                         CTRadioAccessTechnologyCDMAEVDORevA,
                         CTRadioAccessTechnologyCDMAEVDORevB,
                         CTRadioAccessTechnologyeHRPD:
    
                        print("Reachable Via 3G")
    
                    case CTRadioAccessTechnologyLTE:
    
                        print("Reachable Via 4G")
    
                    default:
    
                        fatalError("error")
    
                    }
                }
            }
        }
    

提交回复
热议问题