BOOL success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct if_data *networkStatisc;
float WiFiSent = 0;
float WiFiReceived = 0;
u_int64_t WWANSent = 0;
u_int64_t WWANReceived = 0;
NSString *name=[[[NSString alloc]init]autorelease];
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 = (const struct if_data *) cursor->ifa_data;
//WiFiSent+=networkStatisc->ifi_obytes;
//WiFiReceived+=networkStatisc->ifi_ibytes;
//NSLog(@"WiFiSent %f ==%d",WiFiSent,networkStatisc->ifi_obytes);
//NSLog(@"WiFiReceived %f ==%d",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;
NSLog(@"WWANSent %llu ==%d",WWANSent,networkStatisc->ifi_obytes);
NSLog(@"WWANReceived %llu ==%d",WWANReceived,networkStatisc->ifi_ibytes);
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
我评论了返回wifi使用数据的行,但它仍然返回WWAN界面中使用的wifi数据。
我想只使用手机数据(iPhone上使用3g数据)而不是wifi数据。
由于