Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:23+00:00 2026-05-26T16:41:23+00:00

I’ve searched over this topic but found very few details which were helpful. With

  • 0

I’ve searched over this topic but found very few details which were helpful. With these details I’ve tried to cook some code as follows.

Note: Please compare the details shared in this post with other posts before marking this as DUPLICATE, and not just by the subject.

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

This code collects information of internet usage of an iPhone device (and not my application alone).

Now, if I use internet through WiFi or through 3G, I get the the data (bytes) only in ifi_obytes (sent) and ifi_ibytes (received) but I think I should get WiFi usage in ifi_opackets and ifi_ipackets.

Also wanted to add that if I’m connected to a WiFi network, but am not using internet, I still get value added to ifi_obytes and ifi_ibytes.

May be I’m wrong in the implementation or understanding. Need someone to help me out.


Edit: Instead of AF_LINK I tried AF_INET (sockaddr_in instead of sockaddr_dl). This crashes the application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:41:23+00:00Added an answer on May 26, 2026 at 4:41 pm

    The thing is that pdp_ip0 is one of interfaces, all pdpXXX are WWAN interfaces dedicated to different functions, voicemail, general networking interface.

    I read in Apple forum that :
    The OS does not keep network statistics on a process-by-process basis. As such, there’s no exact solution to this problem. You can, however, get network statistics for each network interface.

    In general en0 is your Wi-Fi interface and pdp_ip0 is your WWAN interface.

    There is no good way to get information wifi/cellular network data since, particular date-time!

    Data statistic (ifa_data->ifi_obytes and ifa_data->ifi_ibytes) are stored from previous device reboot.

    I don’t know why, but ifi_opackets and ifi_ipackets are shown just for lo0 (I think its main interface ).

    Yes. Then device is connected via WiFi and doesn’t use internet if_iobytes values still come because this method provides network bytes exchanges and not just internet.

    #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)};
    }
    

    Improved copy/paste support !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.