iOS检测3G或WiFi
我不知道这是否可能,但我有这种情况。
我有一个网站显示在我的UIWebView中有一个UISegmentedController设置的链接。 他们的网站可以检测你是否在无线networking或3Gnetworking上。
现在分段控制器指向2个不同的页面。 1 – 一个iPhone友好的login屏幕2 – 主页,一旦你login。
现在这是一个问题:
我可以编程我的应用程序,以检测是否在WiFi或3G(我知道你可以做到这一点),但基于答案去1或2段
像这样:
if(iPhone device is on 3g) { Go to Segment 1; } else { Go to Segment 0; }
使用苹果在这里提供的代码
Reachability *reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifier]; NetworkStatus status = [reachability currentReachabilityStatus]; if(status == NotReachable) { //No internet } else if (status == ReachableViaWiFi) { //WiFi } else if (status == ReachableViaWWAN) { //3G }
如果你不想导入Reachability库或者处理通知,你可以使用这个简单的同步方法:
typedef enum { ConnectionTypeUnknown, ConnectionTypeNone, ConnectionType3G, ConnectionTypeWiFi } ConnectionType; + (ConnectionType)connectionType { SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, "8.8.8.8"); SCNetworkReachabilityFlags flags; BOOL success = SCNetworkReachabilityGetFlags(reachability, &flags); CFRelease(reachability); if (!success) { return ConnectionTypeUnknown; } BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0); BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0); BOOL isNetworkReachable = (isReachable && !needsConnection); if (!isNetworkReachable) { return ConnectionTypeNone; } else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) { return ConnectionType3G; } else { return ConnectionTypeWiFi; } }
导入苹果的可达性 ,试试这个,
#import "Reachability.h" #import <CoreTelephony/CTTelephonyNetworkInfo.h> //Try this Reachability *reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifier]; NetworkStatus status = [reachability currentReachabilityStatus]; if(status == NotReachable) { NSLog(@"none"); //No internet } else if (status == ReachableViaWiFi) { NSLog(@"Wifi"); //WiFi } else if (status == ReachableViaWWAN) { NSLog(@"WWAN"); //connection type CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init]; _carrier = [[netinfo subscriberCellularProvider] carrierName]; if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) { NSLog(@"2G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) { NSLog(@"2G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) { NSLog(@"3G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) { NSLog(@"3G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) { NSLog(@"3G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) { NSLog(@"2G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) { NSLog(@"3G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) { NSLog(@"3G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { NSLog(@"3G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) { NSLog(@"3G"); } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { NSLog(@"4G"); } }
参考文献(链接可能在未来打破):
- iOS检测3G或WiFi
- 检测运营商连接types(3G / EDGE / GPRS)
- 1G,2G,3G,4G和5G有什么区别?
- CDMA2000
- 维基百科2G
-
CDMA和WCDMAnetworking模拟器有什么区别?
-
什么是“EV-DO Rev.A”?
我做了一个非常简单的基于块的Reachability包装,剥离了所有过时的C-like Reachability代码,倒入更多的cocoaforms。
用法如下:
[EPPZReachability reachHost:hostNameOrIPaddress completition:^(EPPZReachability *reachability) { if (reachability.reachableViaCellular) [self doSomeLightweightStuff]; }];
请参阅eppz!博客上 日常使用块的Reachability ,或直接从GitHub上的eppz!reachability获取它。
它也适用于IP地址 ,这是一个非常罕见的Reachability包装function。
为了迅速,我们可以使用:
func getNetworkType()->String { do{ let reachability:Reachability = try Reachability.reachabilityForInternetConnection() do{ try reachability.startNotifier() let status = reachability.currentReachabilityStatus if(status == .NotReachable){ return "" }else if (status == .ReachableViaWiFi){ return "Wifi" }else if (status == .ReachableViaWWAN){ let networkInfo = CTTelephonyNetworkInfo() let carrierType = networkInfo.currentRadioAccessTechnology switch carrierType{ case CTRadioAccessTechnologyGPRS?,CTRadioAccessTechnologyEdge?,CTRadioAccessTechnologyCDMA1x?: return "2G" case CTRadioAccessTechnologyWCDMA?,CTRadioAccessTechnologyHSDPA?,CTRadioAccessTechnologyHSUPA?,CTRadioAccessTechnologyCDMAEVDORev0?,CTRadioAccessTechnologyCDMAEVDORevA?,CTRadioAccessTechnologyCDMAEVDORevB?,CTRadioAccessTechnologyeHRPD?: return "3G" case CTRadioAccessTechnologyLTE?: return "4G" default: return "" } // Get carrier name }else{ return "" } }catch{ return "" } }catch{ return "" } }
#import <ifaddrs.h> #import <arpa/inet.h> BOOL CheckWiFi() { struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; BOOL hasWifi = NO; int err = getifaddrs(&interfaces); if(err == 0) { temp_addr = interfaces; while(temp_addr) { if(temp_addr->ifa_addr->sa_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)temp_addr->ifa_addr; if(memcmp(temp_addr->ifa_name, "en", 2) == 0) { hasWifi = YES; break; } } temp_addr = temp_addr->ifa_next; } } freeifaddrs(interfaces); return hasWifi; }
要检查你是否在WiFi,这节省了昂贵的连接检查。 检查ifa_name“bridge”以检查Internet共享。
类方法如下
+(NSString*)connectedNetworkType { Reachability *reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifier]; NetworkStatus status = [reachability currentReachabilityStatus]; if(status == NotReachable) { NSLog(@"none"); //No internet } else if (status == ReachableViaWiFi) { NSLog(@"Wifi"); //WiFi return @"Wifi"; } else if (status == ReachableViaWWAN){ NSLog(@"WWAN"); //connection type CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init]; // _carrier = [[netinfo subscriberCellularProvider] carrierName]; if (([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x])) { NSLog(@"2G"); return @"2G"; } else if (([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) ||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD])){ NSLog(@"3G"); return @"3G"; } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { NSLog(@"4G"); return @"4G"; } } return @"-1";//default unknown }
这里是iOS 6的更新版本,包括苹果的SimplePing。 这是ARC兼容,我开始从另一个人的修复可达性。 http://elbsolutions.com/projects/reachability-with-simpleping-wrapper/
我希望这可以帮助别人。
如果您使用Xamarin或Monotouch,则可以使用Xamarin GitHub存储库中的Reachability自适应类:
https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs
所以把它添加到你的项目,并调用Reachability.InternetConnectionStatus()
使用这个,与可达性和易于使用,只需几行代码集成。 有一个callback函数告诉你什么时候连接改变了http://huytd.github.io/datatify/