實時監測每秒上行下行流量,一款 iOS Today Widget 監測器Demo,還可以可以監測內存大小、存儲空間等信息


主要介紹每秒上行、下行流量,結尾有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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM