How to track network traffic in iOS using Apple's public API, not private?

I am developing an iOS application that calculates download and download speeds using network traffic, but still I ran into the problem of calculating the bandwidth of network traffic. How can I get network traffic using Apple's open API and not a private API ?. I am currently using the code below to read and write bytes on the network:

-(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];

}

How can I capture and calculate the download and download speed passively?

+4
source share

Source: https://habr.com/ru/post/1613351/


All Articles