iPhone数据使用跟踪/监控我已经搜索过这个话题,但是很少发现有帮助的细节。关于这些细节,我尝试编写如下代码。注:请将此帖子中分享的细节与其他帖子进行比较,然后再将其标记为重复,而不仅仅是主题。- (NSArray *)getDataCountersForType:(int)type {
BOOL success;
struct ifaddrs *addrs = nil;
const struct ifaddrs *cursor = nil;
const struct sockaddr_dl *dlAddr = nil;
const struct if_data *networkStatisc = nil;
int dataSent = 0;
int dataReceived = 0;
success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != NULL) {
if (cursor->ifa_addr->sa_family == AF_LINK) {
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
networkStatisc = (const struct if_data *) cursor->ifa_data;
if (type == WiFi) {
dataSent += networkStatisc->ifi_opackets;
dataReceived += networkStatisc->ifi_ipackets;
}
else if (type == WWAN) {
dataSent += networkStatisc->ifi_obytes;
dataReceived += networkStatisc->ifi_ibytes;
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return [NSArray arrayWithObjects:[NSNumber numberWithInt:dataSent], [NSNumber numberWithInt:dataReceived], nil]; }此代码收集iPhone设备的互联网使用信息(而不仅仅是我的应用程序)。现在,如果我通过WiFi或3G使用互联网,我只能在ifi_obytes(发送)和ifi_ibytes(收到)但我想我应该在ifi_opackets和ifi_ipackets.还想说,如果我连接到WiFi网络,但不使用互联网,我仍然可以获得增值ifi_obytes和ifi_ibytes.也许我在实现或理解上错了。需要有人帮我。编辑:而不是AF_LINK我试过AF_INET (sockaddr_in而不是sockaddr_dl)。这会使应用程序崩溃。
3 回答
胡子哥哥
TA贡献1825条经验 获得超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)};}
改进的复制/粘贴支持!
喵喔喔
TA贡献1735条经验 获得超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 关注
- 741 浏览
添加回答
举报
0/150
提交
取消