简体   繁体   English

iPhone主动网络类型(2G,3G,WiFi)

[英]iPhone active network type (2G, 3G, WiFi)

Does anyone know how to determine the active network type at the specific moment: 2G , 3G or WiFi . 有谁知道如何确定特定时刻的活动网络类型: 2G3GWiFi

For example, at a specific moment there could be enabled 3G , but the used network type could be 2G . 例如,在特定时刻可能启用3G ,但使用的网络类型可能是2G

The SCNetworkReachability interface can help you with that. SCNetworkReachability接口可以帮助您。 Basically, you create a so-called reachability reference and then call SCNetworkReachabilityGetFlags on it to get information about the connection. 基本上,您创建一个所谓的可达性引用,然后在其上调用SCNetworkReachabilityGetFlags以获取有关连接的信息。

The returned flags include kSCNetworkReachabilityFlagsIsWWAN , which tells you whether you are connected via WiFi or the cell network. 返回的标志包括kSCNetworkReachabilityFlagsIsWWAN ,它告诉您是通过WiFi还是通过蜂窝网络连接。 AFAIK it cannot be used to tell the difference between 2G and 3G, though. 但AFAIK不能用来说明2G和3G之间的区别。

See Apple's Reachability sample app for an implementation. 有关实施,请参阅Apple的Reachability示例应用程序 In most cases, you should be able to directly use the included Reachability class in your project. 在大多数情况下,您应该能够直接在项目中使用包含的Reachability类。

Go to the Apple Developer site, and download a sample project called "Reachability" 转到Apple Developer站点,下载名为“Reachability”的示例项目

It provides an example of what you would like to do. 它提供了您想要做的事情的示例。

It is worth noting that I don't believe that you can tell the difference between EDGE(2G) and a 3G connection. 值得注意的是,我不相信你可以区分EDGE(2G)和3G连接。 It's either WiFi or WWAN. 这是WiFi或WWAN。

This is the way to find the network mode(2G,3G,4G or wifi) of your device in swift. 这是在swift中查找设备的网络模式(2G,3G,4G或wifi)的方法。

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

                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM