3 回答

TA貢獻1825條經(jīng)驗 獲得超6個贊
pdp_ip0
pdpXXX
WWAN
en0
Wi-Fi
pdp_ip0
WWAN
ifa_data->ifi_obytes
ifa_data->ifi_ibytes
ifi_opackets
ifi_ipackets
lo0
WiFi
if_iobytes
#include <net/if.h>#include <ifaddrs.h>static NSString *const DataCounterKeyWWANSent = @"WWANSent"; static NSString *const DataCounterKeyWWANReceived = @"WWANReceived";static NSString *const DataCounterKeyWiFiSent = @"WiFiSent"; static NSString *const DataCounterKeyWiFiReceived = @"WiFiReceived";NSDictionary *DataCounters(){ struct ifaddrs *addrs; const struct ifaddrs *cursor; u_int32_t WiFiSent = 0; u_int32_t WiFiReceived = 0; u_int32_t WWANSent = 0; u_int32_t WWANReceived = 0; if (getifaddrs(&addrs) == 0) { cursor = addrs; while (cursor != NULL) { if (cursor->ifa_addr->sa_family == AF_LINK) {#ifdef DEBUG const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data; if (ifa_data != NULL) { NSLog(@"Interface name %s: sent %tu received %tu",cursor->ifa_name,ifa_data->ifi_obytes,ifa_data->ifi_ibytes); }#endif // name of interfaces: // en0 is WiFi // pdp_ip0 is WWAN NSString *name = @(cursor->ifa_name); if ([name hasPrefix:@"en"]) { const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data; if (ifa_data != NULL) { WiFiSent += ifa_data->ifi_obytes; WiFiReceived += ifa_data->ifi_ibytes; } } if ([name hasPrefix:@"pdp_ip"]) { const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data; if (ifa_data != NULL) { WWANSent += ifa_data->ifi_obytes; WWANReceived += ifa_data->ifi_ibytes; } } } cursor = cursor->ifa_next; } freeifaddrs(addrs); } return @{DataCounterKeyWiFiSent : @(WiFiSent), DataCounterKeyWiFiReceived : @(WiFiReceived), DataCounterKeyWWANSent : @(WWANSent), DataCounterKeyWWANReceived : @(WWANReceived)};}
改進的復(fù)制/粘貼支持!

TA貢獻1735條經(jīng)驗 獲得超5個贊
0
4 GB
ifi_obytes
ifi_ibytes
uint_32
4294967295
.
unsigned int
int
ifi_obytes
unsigned int sent = 0;sent += networkStatisc->ifi_obytes;
- 3 回答
- 0 關(guān)注
- 788 瀏覽
添加回答
舉報