IOS traffic statistics and ios traffic statistics

Source: Internet
Author: User

IOS traffic statistics and ios traffic statistics

During development, you sometimes need to obtain traffic statistics. The study found that the system network interface information is obtained through the getifaddrs function. The network interface information is contained in the if_data field. However, I only care about ifi_ibytes, ifi_obytes, it should be the number of received bytes and the number of sent bytes, which add up to the traffic. We also found that the interface names include en, pdp_ip, lo, and other forms. en should be wifi, pdp_ip is about 3G or gprs, and lo is the loopback interface, the names can be used for separate statistics.

  

1. Import necessary header files
#include <ifaddrs.h>#include <sys/socket.h>#include <net/if.h>

 

2. Get 3G or GPRS traffic
// Get 3G or GPRS traffic + (NSString *) getupls3gflowiobytes {struct ifaddrs * ifa_list = 0, * ifa; if (getifaddrs (& ifa_list) =-1) {return 0;} uint32_t iBytes = 0; uint32_t oBytes = 0; for (ifa = ifa_list; ifa = ifa-> ifa_next) {if (AF_LINK! = Ifa-> ifa_addr-> sa_family) continue; if (! (Ifa-> ifa_flags & IFF_UP )&&! (Ifa-> ifa_flags & IFF_RUNNING) continue; if (ifa-> ifa_data = 0) continue; // 3G or GPRS if (! Strcmp (ifa-> ifa_name, "pdp_ip0") {struct if_data * if_data = (struct if_data *) ifa-> ifa_data; iBytes + = if_data-> ifi_ibytes; oBytes + = if_data-> ifi_obytes; NSLog (@ "% s: iBytes is % d, oBytes is % d", ifa-> ifa_name, iBytes, oBytes );}} freeifaddrs (ifa_list); uint32_t bytes = 0; bytes = iBytes + oBytes; // convert bytes units to if (bytes <1024) // B {return [NSString stringWithFormat: @ "% dB", bytes];} else if (bytes> = 1024 & bytes <1024*1024) // KB {return [NSString stringWithFormat: @ "%. 1fKB ", (double) bytes/1024];} else if (bytes> = 1024*1024 & bytes <1024*1024*1024) // MB {return [NSString stringWithFormat: @ "%. 2fMB ", (double) bytes/(1024*1024)];} else // GB {return [NSString stringWithFormat: @" %. 3fGB ", (double) bytes/(1024*1024*1024)] ;}}

 

3. Get Wifi traffic
// Get Wifi traffic + (NSString *) getgprs swififlowiobytes {struct ifaddrs * ifa_list = 0, * ifa; if (getifaddrs (& ifa_list) =-1) {return 0 ;} uint32_t iBytes = 0; uint32_t oBytes = 0; for (ifa = ifa_list; ifa = ifa-> ifa_next) {if (AF_LINK! = Ifa-> ifa_addr-> sa_family) continue; if (! (Ifa-> ifa_flags & IFF_UP )&&! (Ifa-> ifa_flags & IFF_RUNNING) continue; if (ifa-> ifa_data = 0) continue; // Wifi if (strncmp (ifa-> ifa_name, "lo ", 2) {struct if_data * if_data = (struct if_data *) ifa-> ifa_data; iBytes + = if_data-> ifi_ibytes; oBytes + = if_data-> ifi_obytes; NSLog (@ "% s: iBytes is % d, oBytes is % d", ifa-> ifa_name, iBytes, oBytes) ;}} freeifaddrs (ifa_list); uint32_t bytes = 0; bytes = iBytes + oBytes; NSLog (@ "% d", bytes); // convert bytes units to if (bytes <1024) // B {return [NSString stringWithFormat: @ "% dB", bytes];} else if (bytes> = 1024 & bytes <1024*1024) // KB {return [NSString stringWithFormat: @ "%. 1fKB ", (double) bytes/1024];} else if (bytes> = 1024*1024 & bytes <1024*1024*1024) // MB {return [NSString stringWithFormat: @ "%. 2fMB ", (double) bytes/(1024*1024)];} else // GB {return [NSString stringWithFormat: @" %. 3fGB ", (double) bytes/(1024*1024*1024)] ;}}

 

Note: 1. Read the network interface information of the system to obtain the traffic information of the current iphone device. The statistics are collected from the previous boot to the present.

2. The statistical results of the preceding two methods are in string format.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.