iOS获取流量参考

时间:2023-03-10 04:50:45
iOS获取流量参考

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

倒入库:

  1. SystemConfiguration.framework

加入头文件:

  1. #include <ifaddrs.h>
  2. #include <sys/socket.h>
  3. #include <net/if.h>

流量统计功能

  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. -(void)checkNetworkflow{
  21. struct ifaddrs *ifa_list = 0, *ifa;
  22. if (getifaddrs(&ifa_list) == -1)
  23. {
  24. return;
  25. }
  26. uint32_t iBytes     = 0;
  27. uint32_t oBytes     = 0;
  28. uint32_t allFlow    = 0;
  29. uint32_t wifiIBytes = 0;
  30. uint32_t wifiOBytes = 0;
  31. uint32_t wifiFlow   = 0;
  32. uint32_t wwanIBytes = 0;
  33. uint32_t wwanOBytes = 0;
  34. uint32_t wwanFlow   = 0;
  35. struct timeval time ;
  36. for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
  37. {
  38. if (AF_LINK != ifa->ifa_addr->sa_family)
  39. continue;
  40. if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))
  41. continue;
  42. if (ifa->ifa_data == 0)
  43. continue;
  44. // Not a loopback device.
  45. // network flow
  46. if (strncmp(ifa->ifa_name, "lo", 2))
  47. {
  48. struct if_data *if_data = (struct if_data *)ifa->ifa_data;
  49. iBytes += if_data->ifi_ibytes;
  50. oBytes += if_data->ifi_obytes;
  51. allFlow = iBytes + oBytes;
  52. time = if_data->ifi_lastchange;
  53. // NSLog(@"1111===%s :iBytes is %d, oBytes is %d", ifa->ifa_name, iBytes, oBytes);
  54. }
  55. //<span style="font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; ">WIFI流量统计功能</span>
  56. if (!strcmp(ifa->ifa_name, "en0"))
  57. {
  58. struct if_data *if_data = (struct if_data *)ifa->ifa_data;
  59. wifiIBytes += if_data->ifi_ibytes;
  60. wifiOBytes += if_data->ifi_obytes;
  61. wifiFlow    = wifiIBytes + wifiOBytes;
  62. }
  63. //<span style="font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; ">3G和GPRS流量统计</span>
  64. if (!strcmp(ifa->ifa_name, "pdp_ip0"))
  65. {
  66. struct if_data *if_data = (struct if_data *)ifa->ifa_data;
  67. wwanIBytes += if_data->ifi_ibytes;
  68. wwanOBytes += if_data->ifi_obytes;
  69. wwanFlow    = wwanIBytes + wwanOBytes;
  70. //NSLog(@"111122===%s :iBytes is %d, oBytes is %d",  ifa->ifa_name, iBytes, oBytes);
  71. }
  72. }
  73. freeifaddrs(ifa_list);
  74. NSString *changeTime=[NSString stringWithFormat:@"%s",ctime(&time)];
  75. NSLog(@"changeTime==%@",changeTime);
  76. NSString *receivedBytes= [self bytesToAvaiUnit:iBytes];
  77. NSLog(@"receivedBytes==%@",receivedBytes);
  78. NSString *sentBytes       = [self bytesToAvaiUnit:oBytes];
  79. NSLog(@"sentBytes==%@",sentBytes);
  80. NSString *networkFlow      = [self bytesToAvaiUnit:allFlow];
  81. NSLog(@"networkFlow==%@",networkFlow);
  82. NSString *wifiReceived   = [self bytesToAvaiUnit:wifiIBytes];
  83. NSLog(@"wifiReceived==%@",wifiReceived);
  84. NSString *wifiSent       = [self bytesToAvaiUnit: wifiOBytes];
  85. NSLog(@"wifiSent==%@",wifiSent);
  86. NSString *wifiBytes      = [self bytesToAvaiUnit:wifiFlow];
  87. NSLog(@"wifiBytes==%@",wifiBytes);
  88. NSString *wwanReceived   = [self bytesToAvaiUnit:wwanIBytes];
  89. NSLog(@"wwanReceived==%@",wwanReceived);
  90. NSString *wwanSent       = [self bytesToAvaiUnit:wwanOBytes];
  91. NSLog(@"wwanSent==%@",wwanSent);
  92. NSString *wwanBytes      = [self bytesToAvaiUnit:wwanFlow];
  93. NSLog(@"wwanBytes==%@",wwanBytes);
  94. }

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

  1. [self checkNetworkflow];

结果:

  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流量统计。