我正在开发一个iOS应用程序,它使用网络流量计算上传和下载速度,但我仍然遇到计算网络流量吞吐量的问题。如何使用Apple的公共API代替私有API来获取网络流量? 目前我使用下面的代码来获取网络上的字节读取和写入:
-(NSArray *)getPassiveCounters{
NSString *name;
success = getifaddrs(&addrs) == 0;
if (success)
{
cursor = addrs;
while (cursor != NULL)
{
name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
if (cursor->ifa_addr->sa_family == AF_LINK)
{
if ([name hasPrefix:@"en"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WiFiSent+=networkStatisc->ifi_obytes;
WiFiReceived+=networkStatisc->ifi_ibytes;
}
if ([name hasPrefix:@"pdp_ip"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WWANSent+=networkStatisc->ifi_obytes;
WWANReceived+=networkStatisc->ifi_ibytes;
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return [NSArray arrayWithObjects:[NSNumber numberWithUnsignedLongLong:WiFiSent+WWANSent], [NSNumber numberWithUnsignedLongLong:WiFiReceived+WWANReceived], nil];
}
如何被动地捕获和计算上传和下载速度?