0

I am trying to track data usage every day.I got some hint for tracking data usage, but not getting how to calculate data usage for 1 day. IOS app "My Data Manager" tracks data usage and gives usage on daily/monthly basis.

Link: http://www.mobidia.com/

What may be the logic behind,getting data usage for specific time interval(1 day/1 month)?

Data usage tracking code:

- (NSArray *)getDataCounters
{
    BOOL   success;
    struct ifaddrs *addrs;
    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc; 

    int WiFiSent = 0;
    int WiFiReceived = 0;
    int WWANSent = 0;
    int 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 %d ==%d",WiFiSent,networkStatisc->ifi_obytes);
                    NSLog(@"WiFiReceived %d ==%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 %d ==%d",WWANSent,networkStatisc->ifi_obytes);
                    NSLog(@"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes);
                } 
            }

            cursor = cursor->ifa_next;
        }

        freeifaddrs(addrs);
    }       

    return [NSArray arrayWithObjects:[NSNumber numberWithInt:WiFiSent], [NSNumber numberWithInt:WiFiReceived],[NSNumber numberWithInt:WWANSent],[NSNumber numberWithInt:WWANReceived], nil];
}
  • 1
    That's not your own code. It's an exact copy from http://stackoverflow.com/questions/7946699/iphone-data-usage-tracking-monitoring/8014012#8014012. -1 for that... – El Tomato Jan 09 '14 at 10:03

1 Answers1

0

As far as I know there is no way to get that information from a period of time, you can only get the current data. So, you should track the data usage and store it according to the current date. Then you can show the information previously tracked.

Pablo A.
  • 2,042
  • 1
  • 17
  • 27