首页 > 代码库 > Objective-C determine data network type of the iOS device
Objective-C determine data network type of the iOS device
Im on an application that receive data from server, the problem is when user connect to cellular data
(Not 3G
or WIFI
), it take ages to receive data.
i had implemented this code from this Answer but im not sure if it is effective or not, sometimes it‘s giving me an accurate type, and sometimes it don‘t.
here is my code:
- (void)newtworkType {NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];NSNumber *dataNetworkItemView = nil;for (id subview in subviews) { if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) { dataNetworkItemView = subview; break; }}switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) { case 0: NSLog(@"No wifi or cellular"); break; case 1: NSLog(@"2G"); break; case 2: NSLog(@"3G"); break; case 3: NSLog(@"4G"); break; case 4: NSLog(@"LTE"); break; case 5: NSLog(@"Wifi"); break; default: break;}}
Make sure that the Status bar
is not hidden in your application. if it‘s not visible it will always return No wifi or cellular
because your code reads the text in the Status bar
thats all.
this is the best way to solve your problem, just make the Status bar
not hidden then the application will get the text about the network type.
判断网络类型
GPRS 2G(2.5) General Packet Radia Service 114kbps
EDGE 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
UMTS 3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
CDMA 2G 电信 Code Division Multiple Access 码分多址
EVDO_0 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
EVDO_A 3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
1xRTT 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
HSDPA 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
HSUPA 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
HSPA 3G (分HSDPA,HSUPA) High Speed Packet Access
IDEN 2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
EVDO_B 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
LTE 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G
EHRPD 3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
HSPAP 3G HSPAP 比 HSDPA 快些
From iOS 7 on you can use:
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);}];
I have also found this to detect a slow or fast connection:
- (BOOL)isFast:(NSString*)radioAccessTechnology { if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) { return NO; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) { return NO; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { return YES; } return YES;}