在开发中要获取网络类型是很简单的,导入Reachability直接获取状态就行了,现在我们要做一个类似下载器的那种实时把上传下载速度显示出来。
需要用到的头文件
要测速度所以必须要有一个定时器,咱们为了不耗用户的流量,取的是数据的总量,然后减去上一次的检测的总量,得出的就是速度。网络现在分为wifi以及wwan两种类型。
首先头文件.h建立一个检测的数据类
@interface MonitorData : NSObject @property (assign, nonatomic) float wwanSend; @property (assign, nonatomic) float wwanReceived; @property (assign, nonatomic) float wifiSend; @property (assign, nonatomic) float wifiReceived; @end
然后建立一个检测类
@interface MonitorFlow : NSObject //开始检测 - (void)startMonitor; //停止检测 - (void)stopMonitor; @end 实现文件.M //成员变量是内部可见的 @interface MonitorFlow () @property (strong,nonatomic) NSTimer *timer; @property (assign, nonatomic) float tempWWANReceived; @property (assign, nonatomic) float tempWWANSend; @property (assign, nonatomic) float tempWifiReceived; @property (assign, nonatomic) float tempWifiSend; @end 直接把代码附上,里面有注释 @implementation MonitorFlow - (void)startMonitor { [self currentFlow]; self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(refreshFlow) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)stopMonitor{ [self.timer invalidate]; } - (void)refreshFlow{ // 上传、下载 //不需要连通网络获取的是总的数据 Reachability *reachability = [Reachability reachabilityWithHostName:@"Yes-Cui"]; MonitorData *monitor = [self getMonitorDataDetail]; switch (reachability.currentReachabilityStatus) { case ReachableViaWiFi: { float wifiSend = monitor.wifiSend - self.tempWifiSend; float wifiReceived = monitor.wifiReceived - self.tempWifiReceived; NSLog(@"wifi上传速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wifiSend]); NSLog(@"wifi下载速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wifiReceived]); } break; case ReachableViaWWAN: { float wwanSend = monitor.wwanSend - self.tempWWANReceived; float wwanReceived = monitor.wifiReceived - self.tempWWANSend; NSLog(@"wwan上传速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wwanSend]); NSLog(@"wwan下载速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wwanReceived]); } break; default: { NSLog(@"无网络"); } break; } [self currentFlow]; } //赋值当前流量 - (void)currentFlow{ MonitorData *monitor = [self getMonitorDataDetail]; self.tempWifiSend = monitor.wifiSend; self.tempWifiReceived = monitor.wifiReceived; self.tempWWANSend = monitor.wwanSend; self.tempWWANReceived = monitor.wwanReceived; } //上传、下载总额流量 - (MonitorData *)getMonitorDataDetail { BOOL success; struct ifaddrs *addrs; struct ifaddrs *cursor; struct if_data *networkStatisc; long tempWiFiSend = 0; long tempWiFiReceived = 0; long tempWWANSend = 0; long tempWWANReceived = 0; NSString *dataName; success = getifaddrs(&addrs) == 0; if (success) { cursor = addrs; while (cursor != NULL) { dataName = [NSString stringWithFormat:@"%s",cursor->ifa_name]; if (cursor->ifa_addr->sa_family == AF_LINK) { if ([dataName hasPrefix:@"en"]) { networkStatisc = (struct if_data *) cursor->ifa_data; tempWiFiSend += networkStatisc->ifi_obytes; tempWiFiReceived += networkStatisc->ifi_ibytes; } if ([dataName hasPrefix:@"pdp_ip"]) { networkStatisc = (struct if_data *) cursor->ifa_data; tempWWANSend += networkStatisc->ifi_obytes; tempWWANReceived += networkStatisc->ifi_ibytes; } } cursor = cursor->ifa_next; } freeifaddrs(addrs); } MonitorData *monitorData = [MonitorData new]; monitorData.wifiSend = tempWiFiSend/1024; monitorData.wifiReceived = tempWiFiReceived/1024; monitorData.wwanSend = tempWWANSend/1024; monitorData.wwanReceived = tempWWANReceived/1024; return monitorData; } @end
來源:https://www.jianshu.com/p/42ab08c998ae