• iOS获取流量参考


    通过读取系统网络接口信息,获取当前iphone设备的流量相关信息,统计的是上次开机至今的流量信息. 

    倒入库:

    [html] view plain copy
    1. SystemConfiguration.framework  

    加入头文件:

    [html] view plain copy
    1. #include <ifaddrs.h>  
    2. #include <sys/socket.h>  
    3. #include <net/if.h>  

    流量统计功能

    [html] view plain copy
    1. -(NSString *)bytesToAvaiUnit:(int)bytes  
    2. {  
    3.     if(bytes 1024)     // B  
    4.     {  
    5.         return [NSString stringWithFormat:@"%dB", bytes];  
    6.     }  
    7.     else if(bytes >= 1024 && bytes 1024 * 1024) // KB  
    8.     {  
    9.         return [NSString stringWithFormat:@"%.1fKB", (double)bytes / 1024];  
    10.     }  
    11.     else if(bytes >= 1024 * 1024 && bytes 1024 * 1024 * 1024)   // MB  
    12.     {  
    13.         return [NSString stringWithFormat:@"%.2fMB", (double)bytes / (1024 * 1024)];  
    14.     }  
    15.     else    // GB  
    16.     {  
    17.         return [NSString stringWithFormat:@"%.3fGB", (double)bytes / (1024 * 1024 * 1024)];  
    18.     }  
    19. }  
    20.   
    21.   
    22. -(void)checkNetworkflow{  
    23.       
    24.     struct ifaddrs *ifa_list = 0, *ifa;  
    25.       
    26.     if (getifaddrs(&ifa_list) == -1)  
    27.           
    28.     {  
    29.           
    30.         return;  
    31.     }  
    32.       
    33.     uint32_t iBytes     = 0;  
    34.       
    35.     uint32_t oBytes     = 0;  
    36.       
    37.     uint32_t allFlow    = 0;  
    38.       
    39.     uint32_t wifiIBytes = 0;  
    40.       
    41.     uint32_t wifiOBytes = 0;  
    42.       
    43.     uint32_t wifiFlow   = 0;  
    44.       
    45.     uint32_t wwanIBytes = 0;  
    46.       
    47.     uint32_t wwanOBytes = 0;  
    48.       
    49.     uint32_t wwanFlow   = 0;  
    50.       
    51.     struct timeval time ;  
    52.       
    53.     for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)  
    54.           
    55.     {  
    56.           
    57.         if (AF_LINK != ifa->ifa_addr->sa_family)  
    58.               
    59.             continue;  
    60.           
    61.         if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))  
    62.               
    63.             continue;  
    64.           
    65.         if (ifa->ifa_data == 0)  
    66.               
    67.             continue;  
    68.         // Not a loopback device.  
    69.           
    70.         // network flow  
    71.           
    72.         if (strncmp(ifa->ifa_name, "lo", 2))  
    73.               
    74.         {  
    75.               
    76.             struct if_data *if_data = (struct if_data *)ifa->ifa_data;  
    77.               
    78.             iBytes += if_data->ifi_ibytes;  
    79.               
    80.             oBytes += if_data->ifi_obytes;  
    81.               
    82.             allFlow = iBytes + oBytes;  
    83.               
    84.             time = if_data->ifi_lastchange;  
    85.               
    86.            // NSLog(@"1111===%s :iBytes is %d, oBytes is %d", ifa->ifa_name, iBytes, oBytes);  
    87.               
    88.         }  
    89.           
    90.         //<span style="font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; ">WIFI流量统计功能</span>  
    91.           
    92.         if (!strcmp(ifa->ifa_name, "en0"))  
    93.               
    94.         {  
    95.             struct if_data *if_data = (struct if_data *)ifa->ifa_data;  
    96.               
    97.             wifiIBytes += if_data->ifi_ibytes;  
    98.               
    99.             wifiOBytes += if_data->ifi_obytes;  
    100.               
    101.             wifiFlow    = wifiIBytes + wifiOBytes;  
    102.               
    103.         }  
    104.           
    105.         //<span style="font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; ">3G和GPRS流量统计</span>  
    106.           
    107.         if (!strcmp(ifa->ifa_name, "pdp_ip0"))  
    108.               
    109.         {  
    110.               
    111.             struct if_data *if_data = (struct if_data *)ifa->ifa_data;  
    112.               
    113.             wwanIBytes += if_data->ifi_ibytes;  
    114.               
    115.             wwanOBytes += if_data->ifi_obytes;  
    116.               
    117.             wwanFlow    = wwanIBytes + wwanOBytes;  
    118.               
    119.             //NSLog(@"111122===%s :iBytes is %d, oBytes is %d",  ifa->ifa_name, iBytes, oBytes);  
    120.               
    121.         }  
    122.           
    123.     }  
    124.       
    125.     freeifaddrs(ifa_list);  
    126.     
    127.       
    128.       
    129.     NSString *changeTime=[NSString stringWithFormat:@"%s",ctime(&time)];  
    130.       
    131.     NSLog(@"changeTime==%@",changeTime);  
    132.     NSString *receivedBytes= [self bytesToAvaiUnit:iBytes];  
    133.       
    134.     NSLog(@"receivedBytes==%@",receivedBytes);  
    135.     NSString *sentBytes       = [self bytesToAvaiUnit:oBytes];  
    136.       
    137.     NSLog(@"sentBytes==%@",sentBytes);  
    138.     NSString *networkFlow      = [self bytesToAvaiUnit:allFlow];  
    139.       
    140.     NSLog(@"networkFlow==%@",networkFlow);  
    141.   
    142.     NSString *wifiReceived   = [self bytesToAvaiUnit:wifiIBytes];  
    143.       
    144.     NSLog(@"wifiReceived==%@",wifiReceived);  
    145.     NSString *wifiSent       = [self bytesToAvaiUnit: wifiOBytes];  
    146.       
    147.     NSLog(@"wifiSent==%@",wifiSent);  
    148.       
    149.     NSString *wifiBytes      = [self bytesToAvaiUnit:wifiFlow];  
    150.       
    151.     NSLog(@"wifiBytes==%@",wifiBytes);  
    152.     NSString *wwanReceived   = [self bytesToAvaiUnit:wwanIBytes];  
    153.       
    154.     NSLog(@"wwanReceived==%@",wwanReceived);  
    155.     NSString *wwanSent       = [self bytesToAvaiUnit:wwanOBytes];  
    156.       
    157.     NSLog(@"wwanSent==%@",wwanSent);  
    158.     NSString *wwanBytes      = [self bytesToAvaiUnit:wwanFlow];  
    159.       
    160.     NSLog(@"wwanBytes==%@",wwanBytes);  
    161. }  

     主要方法就是上面的,然后在你想要知道的结果的地方调用就ok了。

    [html] view plain copy
    1. [self checkNetworkflow];  

    结果:

    [html] view plain copy
    1. 2013-03-30 23:45:33.565 Reachability[2993:707] changeTime==Sat Mar 30 09:52:09 2013  
    2. 2013-03-30 23:45:33.567 Reachability[2993:707] receivedBytes==62.73MB  
    3. 2013-03-30 23:45:33.569 Reachability[2993:707] sentBytes==8.22MB  
    4. 2013-03-30 23:45:33.571 Reachability[2993:707] networkFlow==70.94MB  
    5. 2013-03-30 23:45:33.573 Reachability[2993:707] wifiReceived==55.40MB  
    6. 2013-03-30 23:45:33.575 Reachability[2993:707] wifiSent==5.41MB  
    7. 2013-03-30 23:45:33.577 Reachability[2993:707] wifiBytes==60.81MB  
    8. 2013-03-30 23:45:33.579 Reachability[2993:707] wwanReceived==7.33MB  
    9. 2013-03-30 23:45:33.581 Reachability[2993:707] wwanSent==2.81MB  
    10. 2013-03-30 23:45:33.583 Reachability[2993:707] wwanBytes==10.14MB  

    当然你也可以只统计3G/GPRS流量统计 或者 WIFI流量统计。

  • 相关阅读:
    Java单链表的实现
    leetcode2
    Spring容器中Bean的生命周期
    my-leetcode
    Map及HashMap原理简述
    网站搭建:服务器+域名+备案
    浅谈X-UA-Compatible&viewport
    Hexo框架搭建个人博客
    Redis安装教程
    MySQL安装教程 --- 解压版
  • 原文地址:https://www.cnblogs.com/jx66/p/5750191.html
Copyright © 2020-2023  润新知