首页 文章

如何在没有Reachability类的iPhone中检查互联网连接?

提问于
浏览
0

我想在不使用Reachability Classes的情况下检查iPhone中的互联网连接 . 我希望在我的视图中触发特定事件时不断检查连接 . 我还想确定连接是来自Wifi还是通过2G或3G连接 . 我已经尝试过使用Reachability类 . 但是,如果Wifi处于打开状态,这些类只返回值(虽然从Wifi路由器上拔下了网线) . 我试过了

[Reachability reachabilityForInternetConnection]

[Reachability reachabilityWithHostName:@"www.google.com"];

但是,尽管网络断开,上述方法似乎仍无法正常工作 .

还有什么方法可以确定iOS6中netwrok 2G或3G的类型?我知道我们的核心电话框架仅适用于iOS 7.但我只想知道我是否可以确定移动网络iOS 6.0 . 请帮我 .

1 回答

  • 2

    没有可达性类的工作代码以及在iOS 6中工作的代码:

    - (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"];
    }
    

    And the value keys I've found so far:

    0 =没有wifi或蜂窝1 = 2G及更早? 2 = 3G? 3 = 4G 4 = LTE 5 = Wifi

    或者在iOS7中使用 CoreTelephony 框架

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

相关问题