确定3G与边缘

时间:2011-12-06 13:03:49

标签: objective-c ios

我知道可达性示例允许检测网络是否可以通过Wifi或Cell访问,但有没有办法确定小区连接是通过3G还是EDGE?

5 个答案:

答案 0 :(得分:27)

从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);
}];

my article in objc.io.

上阅读更多内容

答案 1 :(得分:14)

nst代码的边缘简化版本,用于沉默我在XCode 4.5中获得的编译器警告:

- (NSNumber *) dataNetworkTypeFromStatusBar {

    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"]    subviews];
    NSNumber *dataNetworkItemView = nil;

    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }
    return [dataNetworkItemView valueForKey:@"dataNetworkType"];
}

到目前为止我找到的值键:

  • 0 =没有wifi或手机
  • 1 = 2G及更早? (未确认)
  • 2 = 3G? (尚未确认)
  • 3 = 4G
  • 4 = LTE
  • 5 = Wifi

答案 2 :(得分:10)

使用私有API,您可以直接在状态栏中阅读此信息。

https://github.com/nst/MobileSignal/blob/master/Classes/UIApplication+MS.m

+ (NSNumber *)dataNetworkTypeFromStatusBar {

    UIApplication *app = [UIApplication sharedApplication];

    UIStatusBar *statusBar = [app valueForKey:@"statusBar"];

    UIStatusBarForegroundView *foregroundView = [statusBar valueForKey:@"foregroundView"];

    NSArray *subviews = [foregroundView subviews];

    UIStatusBarDataNetworkItemView *dataNetworkItemView = nil;

    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }

    return [dataNetworkItemView valueForKey:@"dataNetworkType"];
}

答案 3 :(得分:7)

telephonyInfo.currentRadioAccessTechnology值:

CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyGPRS          __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyEdge          __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyWCDMA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSDPA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSUPA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMA1x        __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORev0  __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevA  __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevB  __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyeHRPD         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyLTE __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);

我认为这些都是可能的价值。

答案 4 :(得分:0)

不,没有在小区连接中公开检测网络技术的事情。