ios 检测运营商连接类型(3G / EDGE / GPRS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11049660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:48:18  来源:igfitidea点击:

Detect carrier connection type (3G / EDGE / GPRS)

iphoneobjective-ciosnetwork-programmingiphone-privateapi

提问by elp

How can i get the type of connection of a carrier network?

如何获取运营商网络的连接类型?

  • I'm able to get if connection is WIFI or WWAN using Reachabilityclass
  • I'm able to get network flags

    Reachability Flag Status: WR t------ localWiFiStatusForFlags

  • I'm able to get WIFI SSID using CaptiveNetwork

  • 如果连接是 WIFI 或 WWAN,我可以使用Reachability类获得
  • 我能够获得网络标志

    可达标志状态:WR t------ localWiFiStatusForFlags

  • 我可以使用 WIFI SSID CaptiveNetwork

Supported interfaces: ( en0 )

支持的接口:(EN0)

en0 => {  
    BSSID = "xx:xx:xx:xx:xx:xx";  
    SSID = MyWifiNetwork;  
    SSIDDATA = <x1x1x1x1 x1x1x1x1 x1>;  
}  

But i'm not able to differenziate 3G, EDGE or GPRS connection.

但我无法区分 3G、EDGE 或 GPRS 连接。

Any idea also using iOS private API?

任何想法也使用iOS私有API?

thanks.

谢谢。

回答by Ben Groot

From iOS 7 on you can use:

从iOS的7,你可以使用:

CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
[NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification 
                                                object:nil 
                                                 queue:nil 
                                            usingBlock:^(NSNotification *note) 
{
    NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
}];

I have also found this to detect a slow or fast connection:

我还发现这可以检测慢速或快速连接:

- (BOOL)isFast:(NSString*)radioAccessTechnology {
    if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
        return NO;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
        return NO;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
        return YES;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
        return YES;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
        return YES;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
        return NO;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
        return YES;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
        return YES;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
        return YES;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
        return YES;
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
        return YES;
    }

    return YES;
}

回答by elp

Here the OLDsolution, using private API, in particular SoftwareUpdateServices.framework

这里的OLD解决方案,特别是使用私有 APISoftwareUpdateServices.framework

Class NetworkMonitor = NSClassFromString(@"SUNetworkMonitor");
NSLog(@"TYPE: %d", [NetworkMonitor currentNetworkType]);

It returns:

它返回:

0: NO DATA
1: WIFI
2: GPRS/EDGE
3: 3G

0:无数据
1:WIFI
2:GPRS/EDGE
3:3G

hope this helps community.

希望这有助于社区。

回答by cb89

The accepted answer isn't working on iOS 10. I found a workaround and setup a timer in AppDelegate which is checking the property currentRadioAccessTechnology every 5 seconds. Therefore we also need a function to check if WIFI connection is available instead of radio access technology.

接受的答案不适用于 iOS 10。我找到了一种解决方法并在 AppDelegate 中设置了一个计时器,它每 5 秒检查一次属性 currentRadioAccessTechnology。因此,我们还需要一个功能来检查 WIFI 连接是否可用,而不是无线电接入技术。

Check if WIFI Connection is available:

检查WIFI连接是否可用:

class func isConnectedToWlan() -> Bool {
    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, 
                         sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
        
Timer.scheduledTimer(timeInterval: TimeInterval.seconds(5.0), target: self, selector:      
                           #selector(showNetworkMessage), userInfo: nil, repeats: true)
.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) } } var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0) if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false { return false } //Only Working for WIFI let isReachable = flags == .reachable let needsConnection = flags == .connectionRequired return isReachable && !needsConnection }

Setup the timer like this:

像这样设置计时器:

    guard !Reachability.isConnecteToWlan() else {
        //Connected to WLAN
        return
    }
    guard let currentRadioAccessTechnology = info.currentRadioAccessTechnology else {
        // No internet connection
        return
    }
    guard (currentRadioAccessTechnology == CTRadioAccessTechnologyGPRS 
             || currentRadioAccessTechnology == CTRadioAccessTechnologyEdge) else {
        // 3G, LTE fast radio access Technology
        return
    }

    if lastRadioAccessTechnology != nil {
        guard let lastRadioAccessTechnology = lastRadioAccessTechnology, 
            (lastInfo != currentRadioAccessTechnology || 
              lastInfo != currentRadioAccessTechnology) else {
            //Internet connection did not change
            return
        }
    }
    // Internet connection changed to Edge or GPRS
    // Store lastRadioAccessTechnology to check if internet connection changed
    lastRadioAccessTechnology = currentRadioAccessTechnology

Selector which is called every 5 seconds:

每 5 秒调用一次的选择器:

Reachability *reach = [[Reachability alloc]init];
if (reach.internetConnectionStatus == NotReachable) {
    NSLog(@"No Connection Found");
} else if (reach.internetConnectionStatus == ReachableViaCarrierDataNetwork) {
    NSLog(@"3G or Edge");
} else if (reach.internetConnectionStatus == ReachableViaWiFiNetwork) {
    NSLog(@"Wifi Connection");
}
[reach release];

回答by Tiffany

I am working on an iPhone application that requires the ability to recognize which type of internet connection is currently being used (Wifi, 3G, Edge, etc). I found a simple way to check by using Apples Reachability sample code. There seems to be a shortage of information about this online, I hope this can help someone.

我正在开发一个 iPhone 应用程序,它需要能够识别当前使用的互联网连接类型(Wifi、3G、Edge 等)。我找到了一种使用 Apples Reachability 示例代码进行检查的简单方法。网上好像缺少这方面的资料,希望可以帮到大家。

First copy Reachability.m/.h into your project and include #include "Reachability.h" into your class.

首先Reachability.m / .H复制到您的项目,包括的#include“Reachability.h”到类。

##代码##

This code may not be the best way to accomplish this, but it appears to be the most simple approach.

此代码可能不是实现此目的的最佳方法,但它似乎是最简单的方法。