http://code4app.com/snippets/one/iOS%E6%B5%81%E9%87%8F%E7%9B%91%E6%8E%A7/5020ba7a6803fae325000000

1.能够检测iOS的WIFI,3G以及GPS的流量。

-(void)checkNetworkflow{

    struct ifaddrs *ifa_list = 0, *ifa;
    if (getifaddrs(&ifa;_list) == -1)
    {
        return;
    }
      
    uint32_t iBytes     = 0;
    uint32_t oBytes     = 0;
    uint32_t allFlow    = 0;
    uint32_t wifiIBytes = 0;
    uint32_t wifiOBytes = 0;
    uint32_t wifiFlow   = 0;
    uint32_t wwanIBytes = 0;
    uint32_t wwanOBytes = 0;
    uint32_t wwanFlow   = 0;
    struct timeval time ;
      
    for (ifa = ifa_list; ifa; 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;
          
        // Not a loopback device.
        // network flow
        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;
            allFlow = iBytes + oBytes;
            time = if_data->ifi_lastchange;
        }
          
        //wifi flow
        if (!strcmp(ifa->ifa_name, "en0"))  
        {  
            struct if_data *if_data = (struct if_data *)ifa->ifa_data;  
              
            wifiIBytes += if_data->ifi_ibytes;  
            wifiOBytes += if_data->ifi_obytes;
            wifiFlow    = wifiIBytes + wifiOBytes;
        }  
          
        //3G and gprs flow
        if (!strcmp(ifa->ifa_name, "pdp_ip0"))  
        {  
            struct if_data *if_data = (struct if_data *)ifa->ifa_data;  
              
            wwanIBytes += if_data->ifi_ibytes;  
            wwanOBytes += if_data->ifi_obytes;  
            wwanFlow    = wwanIBytes + wwanOBytes;
        }
    }
    freeifaddrs(ifa_list);    
}
 
2.iOS获取设备流量使用情况,iPhone Data Usage Tracking/Monitoring,获取的是开机后WIFI,3G/GPRS网络下使用流量(仅仅只能获取开机后的)。
 
#include <arpa/inet.h>

#include <net/if.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
 
+ (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];
}

iOS流量监控的更多相关文章

  1. iOS流量精灵完结版

    从一开始的激动,到现在的三期完结持续了将近三个半月时间,心态也开始变的坦然. 开发期间没有兑现自己的若言,没有写下所有的感悟和困难.我没有借口可言,唯一能说的只能说自己太懒....哈哈 总体来说流量监 ...

  2. 《UNIX/Linux网络日志分析与流量监控》新书发布

    本书从UNIX/Linux系统的原始日志(Raw Log)采集与分析讲起,逐步深入到日志审计与计算机取证环节.书中提供了多个案例,每个案例都以一种生动的记事手法讲述了网络遭到入侵之后,管理人员开展系统 ...

  3. 《Unix/Linux网络日志分析与流量监控》获2015年度最受读者喜爱的IT图书奖

    <Unix/Linux网络日志分析与流量监控>获2015年度最受读者喜爱的IT图书奖.刊登在<中华读书报>( 2015年01月28日 19 版) 我的2015年新作刊登在< ...

  4. 《Unix/Linux日志分析与流量监控》书稿完成

    <Unix/Linux日志分析与流量监控>书稿完成 近日,历时3年创作的75万字书稿已完成,本书紧紧围绕网络安全的主题,对各种Unix/Linux系统及网络服务日志进行了全面系统的讲解,从 ...

  5. linux系统CPU,内存,磁盘,网络流量监控脚本

    前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...

  6. 从零开始学习iftop流量监控(找出服务器耗费流量最多的ip和端口)

    一.iftop是什么 iftop是类似于top的实时流量监控工具. 作用:监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等 官网:http://www.ex-parrot.com/~p ...

  7. Linux服务器---流量监控bandwidthd

    Bandwidthd Bandwidthd是一款免费的流量监控软件,它可以用图标的方式展现出网络流量行为,并且可区分出ftp.tcp等各种协议的流量. 1.安装一些依赖软件 [root@localho ...

  8. vue table中使用多选的问题(翻页后如何保存已选项),联动echarts图表实现流量监控

    流量监控项目需求: 根据表格数据,添加多选功能,默认全选,根据已选项更新图表视图 1.表格需要多选 2.要联动图表,所以关键是要利用表格多选的触发回调函数 vue table中使用多选: 很简单,只需 ...

  9. linux 网络和流量监控

    1.流量监控 工具iptraf 安装:sudo apt-get install iptraf 运行:sudo iptraf   2.网络监控扫描 参考:https://www.cnblogs.com/ ...

随机推荐

  1. Python爬虫--简单爬取图片

    今天晚上弄了一个简单的爬虫,可以爬取网页的图片,现在现在做一下准备工作. 需要的库:urllib 和 re urllib库可以理解为是一个url下载器,其中有三个重要的方法 urllib.urlope ...

  2. iOS开发中NSLog输出格式大全

    本文的内容是总结了一下iOS开发中NSLog输出格式大全,虽然比较基础,但有总结毕竟会各位正在学习iOS开发的朋友们一些小小的帮助. %@                   对象 %d, %i    ...

  3. 更快的方式实现PHP数组去重

    数组去重的几种快捷方式: 1.//创建一个包含重复值的,一共四个元素的数组   $array = array('green','blue','orange','blue');  // 翻转数组,你将会 ...

  4. linux服务器性能优化

    1.这里的吞吐率特指Web服务器单位时间内处理的请求.       2.压力测试的前提:1>并发用户数 2>总请求数 3>请求资源描述       3.用户平均请求等待时间主要用户衡 ...

  5. Orchard part8

    http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-8 定 ...

  6. jQuery 简单过滤选择器

    <!DOCTYPE HTML> <html> <head> <title> 使用jQuery基本过滤选择器 </title> <scr ...

  7. xcode 创建项目 勾选 git 出现警告

    1. 解决方法: 在mac机上找到终端工具  >> 这里先介绍下使用Command-line. 1,下载Git installer,地址:http://git-scm.com/downlo ...

  8. Digital Roots 1013

    Digital Roots 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:456            测试通过:162 描述 T ...

  9. [转]如何在Java中调用DLL方法

    转载地址:http://developer.51cto.com/art/200906/129773.htm Java语言本身具有跨平台性,如果通过Java调用DLL的技术方便易用,使用Java开发前台 ...

  10. python学习之if语句

    1.if条件表达式判断 ##判断条件是true or false var1=10 if var1: print("true") print(var1) else: print(&q ...