简体   繁体   English

如何在没有可达性类的情况下检查iPhone中的互联网连接?

[英]How to check the internet Connection in iPhone without Reachability classes?

I want to check the internet connection in my iPhone without the use of Reachability Classes. 我想在不使用可达性类别的情况下检查iPhone中的互联网连接。 i want to constantly check the connection when a particular event is triggered in my view. 我想在我的视图中触发特定事件时不断检查连接。 I also want to determine if the connection is from a Wifi or through 2G or 3G connection. 我还想确定连接是来自Wifi还是2G或3G连接。 I have already tried using Reachability classes. 我已经尝试过使用可达性类。 But these classes just return the value if the Wifi is On (Though the net cable is unplugged from the Wifi Router). 但是,如果Wifi处于开启状态,则这些类仅返回值(尽管从Wifi路由器上拔出了网线)。 I have tried 我努力了

[Reachability reachabilityForInternetConnection] [互联网连接的可达性可达性]

and

[Reachability reachabilityWithHostName:@"www.google.com"]; [可达到性reachabilityWithHostName:@“ www.google.com”];

But the above methods doesn't seem to work properly inspite of network disconnections. 但是,尽管网络断开,上述方法似乎仍无法正常工作。

Also is there any way we can determine the type of netwrok 2G or 3G in iOS6? 还有什么办法可以确定iOS6中netwrok 2G或3G的类型? I know that we have Core Telephony Framework that works only in iOS 7. But i just want to know if i can determine the cellular network iOS 6.0. 我知道我们拥有只能在iOS 7中运行的Core Telephony Framework。但是我只想知道我是否可以确定蜂窝网络iOS 6.0。 Please help me. 请帮我。

Working code without reachability classes and also working in iOS 6: 没有可达性类的工作代码,也可以在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 = No wifi or cellular 0 =没有wifi或手机

1 = 2G and earlier? 1 = 2G和更早版本?

2 = 3G? 2 = 3G?

3 = 4G 3 = 4G

4 = LTE 4 = LTE

5 = Wifi 5 = Wifi

Or in iOS7 use CoreTelephony framework 或在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);
}];

暂无
暂无

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

相关问题 Swift 3通过可访问性检查互联网连接 - Swift 3 check internet connection with Reachability iOS / iPhone可达性 - 如何使用Reachability.m / .h检查互联网何时丢失/无法访问 - iOS/iPhone Reachability - How to only check when internet is lost/not reachable using Reachability.m/.h 如何在xcode中使用可达性来评估Internet连接 - How to use Reachability in xcode for assessment Internet Connection 如何检查WatchOS 2连接(可达性)到互联网? - How check WatchOS 2 connected (reachability) to the internet? iOS:如何以最简单的方式测试Internet连接,而不冻结应用程序(没有可达性)? - iOS: How to test Internet connection in the most easy way, without freezing the app (without Reachability)? 如何使用可达性来检查是否存在Internet连接,如果没有,请执行某些操作。 (基本上用作布尔值。) - How do I use Reachability to check if there's an internet connection, and if not, do something. (Basically use as a boolean.) 是否可以检查WiFi是否已连接但是Swift中没有使用可达性的互联网连接? - Is it possible to check if WiFi is connected but there is no internet connection in Swift using Reachability? 使用Reachability类在ios中检查整个应用程序中的活动Internet连接 - Check for active internet connection throughout the app in ios using Reachability class 互联网可达性在iPhone中无法正常工作 - Internet reachability not working properly in iPhone 互联网连接状态快速可达 - Internet connection status swift reachability
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM