通过读取系统网络接口信息,获取当前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流量统计。

iOS获取流量参考的更多相关文章

  1. iOS 获取流量

    #include <arpa/inet.h> #include <net/if.h> #include <ifaddrs.h> #include <net/i ...

  2. iOS获取设备型号、装置类型等信息

    iOS获取设备型号.设备类型等信息 设备标识 关于设备标识,历史上盛行过很多英雄,比如UDID.Mac地址.OpenUDID等,然而他们都陆陆续续倒在了苹果的门下.苹果目前提供了2个方法供App获取设 ...

  3. iOS获取UUID

    转自:<iOS获取设备的唯一标识的方法总结以及最好的方法> 参考:<获取iOS设备唯一标识> 总结一下: 1.代码采用CFUUID+KeyChain的实现方式. 2.CFUUI ...

  4. iOS手机流量抓包rvictl

    移动设备抓包主要方式 一.PC上设置网络共享,生成Wi-Fi热点供移动设备使用,PC上再使用tcpdump.Wireshark等捕获分析: 二.PC上开启http代理工具服务器(如Charles.fi ...

  5. iOS编码规范参考

    目录      注释 1.1  多行注释 1.2  单行注释 1.3  函数的注释   命名 2.1  常量的命名 2.2  函数的命名 2.3  变量的命名 2.3.1  成员变量 2.3.2  公 ...

  6. iOS 网络流量统计

    在开发中,有时候需要获取流量统计信息.研究发现:通过函数getifaddrs来得到系统网络接口的信息,网络接口的信息,包含在if_data字段中, 有很多信息, 但我现在只关心ifi_ibytes,  ...

  7. iOS获取设备唯一标识的8种方法

    8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...

  8. iOS 获取文件的目录路径的几种方法 [转]

    iOS 获取文件的目录路径的几种方法 2 years ago davidzhang iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. d ...

  9. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

随机推荐

  1. android 调用系统的音乐和视频播放器

    package com.eboy.testsystemaudiovideo; import android.app.Activity;import android.content.Intent;imp ...

  2. Javascript备忘复习笔记1

    一.字符串操作 1.大小写 var s = "hello"; undefined g = s.toUpperCase(); "HELLO" g; "H ...

  3. 关于MyBatis mapper的insert, update, delete返回值

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...

  4. 匹配字符串的KMP算法

    其中next序列,表示子串的前后缀最大匹配长度. 例如对于字符串C[], next[i]表示子串c[0 .. i]中, 前缀与后缀的最大匹配长度. 举例如果子串是 abcuab, 其前缀是a, ab, ...

  5. 本地 Maven项目部署到Nexus Repository

    配置Nexus Repository 打开WEB管理界面:http://localhost:8081/nexus/index.html 点击右上角Log In进行登录,默认帐号:admin.密码:ad ...

  6. Delphi项目的构成

    Hello.cfg 項目配置文件 Hello.dof 項目選項文件 Hello.dpr 項目文件 Hello.exe 應用程序 Hello.res 資源文件 HelloWorld.dcu 窗口編譯文件 ...

  7. 微软架构师解读Windows Server 2008 R2新特性

    目前众多企业都开始为自己寻找一个更加适合自身发展的服务器操作平台.微软的Windows Server 2008 R2就是可以为大家解决服务器平台问题.微软最新的服务器平台Windows Server ...

  8. [转]使用 google gson 转换Timestamp或Date类型为JSON字符串.

    创建类型适配类: import java.lang.reflect.Type; import java.sql.Timestamp; import java.text.DateFormat; impo ...

  9. 程序开发使用docker部署

    我们公司自己研发了一套 grand-line 系统,使用 docker 来部署项目. 我是第一批小白鼠,一开始网络差,build 一次要半个小时,连接进入 web shell 也很慢,部署一个微信项目 ...

  10. 用opencv的traincascade训练检测器

    #1,准备正负样本 正样本:可以一张图片上多个sample,也可以一张图片单独成一个sample,准备多个sample.生成描述文件如下所示: 负样本:只要不含正样本,任意图片都可以作为负样本,但是最 ...