主要介绍每秒上行、下行流量,结尾有GitHub源码链接。
关于 Today Widget 的开发⬇️
先看效果图 ⬇️
这里介绍一下上行、下行流量的方法。
方法返回一个数组,分别为本月WIFI下的上行、下行总流量, WWAN下的上行、下行总流量,除以1024后单位是KB。
// 上行、下行流量 - (NSArray *)getDataCounters { BOOL success; struct ifaddrs *addrs; struct ifaddrs *cursor; struct if_data *networkStatisc; long WiFiSent = 0; long WiFiReceived = 0; long WWANSent = 0; long WWANReceived = 0; NSString *name=[[NSString alloc]init]; success = getifaddrs(&addrs) == 0; if (success) { cursor = addrs; while (cursor != NULL) { name=[NSString stringWithFormat:@"%s",cursor->ifa_name]; //NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name); // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN if (cursor->ifa_addr->sa_family == AF_LINK) { if ([name hasPrefix:@"en"]) { networkStatisc = (struct if_data *) cursor->ifa_data; WiFiSent+=networkStatisc->ifi_obytes; WiFiReceived+=networkStatisc->ifi_ibytes; //NSLog(@"WiFiSent %d ==%d",WiFiSent,networkStatisc->ifi_obytes); //NSLog(@"WiFiReceived %d ==%d",WiFiReceived,networkStatisc->ifi_ibytes); } if ([name hasPrefix:@"pdp_ip"]) { networkStatisc = (struct if_data *) cursor->ifa_data; WWANSent+=networkStatisc->ifi_obytes; WWANReceived+=networkStatisc->ifi_ibytes; //NSLog(@"WWANSent %d ==%d",WWANSent,networkStatisc->ifi_obytes); //NSLog(@"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes); } } cursor = cursor->ifa_next; } freeifaddrs(addrs); } return [NSArray arrayWithObjects:[NSNumber numberWithInt:WiFiSent/1024], [NSNumber numberWithInt:WiFiReceived/1024],[NSNumber numberWithInt:WWANSent/1024],[NSNumber numberWithInt:WWANReceived/1024], nil]; }
那么每秒的流量可以这样计算,先写一个定时器,用的 NSTimer,用 CADisplayLink 也可以。
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(refreshV) userInfo:nil repeats:YES]; self.timer = timer;
// CADisplayLink* link = [CADisplayLink displayLinkWithTarget:self selector:@selector(refreshV)];
// [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
// link.frameInterval = 60;
然后在每秒执行的方法里面,减去self.preWifi_S的值,再赋值self.preWifi_S,结果基本就是一秒的流量,注意判断WIFI与WWAN环境,还可以判断下数值大小,让单位变MB等,懒病发作没写。
- (void)refreshV { // NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.gsMonitorDemo"]; // float availableMemory = [userDefaults floatForKey:@"group.availableMemory"]; // 内存、存储 float availableMemory = [self availableMemory]; self.menoryLab.text = [NSString stringWithFormat:@"%.0f MB", availableMemory]; float allMemory = [self getTotalMemorySize]; float memoryPre = (1-availableMemory/allMemory)*100; self.memoryPreLab.text = [NSString stringWithFormat:@"%.2f %%", memoryPre]; if (self.flag) { self.guanGeView.value = memoryPre; } float availableDiskSize = [self getAvailableDiskSize]; self.diskLab.text = [NSString stringWithFormat:@"%.2f GB", availableDiskSize / 1024.0]; // 上行、下行流量 Reachability *reachability = [Reachability reachabilityWithHostName:@"hha"]; if (reachability.currentReachabilityStatus == ReachableViaWiFi) { float wifiS_preSecond = [[self getDataCounters][0] floatValue] - self.preWifi_S; float wifiR_preSecond = [[self getDataCounters][1] floatValue] - self.preWifi_R; self.topLiuLiang.text = [NSString stringWithFormat:@"%.0f KB/s", wifiS_preSecond]; self.downLiuLiang.text = [NSString stringWithFormat:@"%.0f KB/s", wifiR_preSecond]; }else if(reachability.currentReachabilityStatus == ReachableViaWWAN) { float wwanS_preSecond = [[self getDataCounters][2] floatValue] - self.preWWAN_S; float wwanR_preSecond = [[self getDataCounters][3] floatValue] - self.preWWAN_R; self.topLiuLiang.text = [NSString stringWithFormat:@"%.0f KB/s", wwanS_preSecond]; self.downLiuLiang.text = [NSString stringWithFormat:@"%.0f KB/s", wwanR_preSecond]; }else { } [self currentLiuLiang]; float cpuUsage = [self cpu_usage]; self.cpuLab.text = [NSString stringWithFormat:@"%.1f%%", cpuUsage]; } // 赋值当前流量 - (void)currentLiuLiang { NSNumber *wifiSendNumber = [self getDataCounters][0]; float wifiS = [wifiSendNumber floatValue]; self.preWifi_S = wifiS; NSNumber *wifiReceived = [self getDataCounters][1]; float wifiR = [wifiReceived floatValue]; self.preWifi_R = wifiR; NSNumber *wwanSendNumber = [self getDataCounters][2]; float wwanS = [wwanSendNumber floatValue]; self.preWWAN_S = wwanS; NSNumber *wwanReceived = [self getDataCounters][3]; float wwanR = [wwanReceived floatValue]; self.preWWAN_R = wwanR; }
右侧的表盘是用一个第三方用Quartz2D画出来的,具体可见代码。
https://github.com/alwaysDB/ZYDMonitor