[英]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.