美文网首页
iOS 统计手机流量

iOS 统计手机流量

作者: MccReeee | 来源:发表于2017-09-21 17:50 被阅读41次
//
//  MRTrack.h
//  MRTrack
//
//  Created by MccReeG on 2017/9/21.
//  Copyright © 2017年 MccReeG. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MRTrack : NSObject

+ (NSDictionary *)trackDataBytes;

@end

//
//  MRTrack.m
//  MRTrack
//
//  Created by MccReeG on 2017/9/21.
//  Copyright © 2017年 MccReeG. All rights reserved.
//

#import "MRTrack.h"
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <net/if_dl.h>

static NSString *const DataCounterKeyWWANSent = @"WWANSent";
static NSString *const DataCounterKeyWWANReceived = @"WWANReceived";
static NSString *const DataCounterKeyWiFiSent = @"WiFiSent";
static NSString *const DataCounterKeyWiFiReceived = @"WiFiReceived";

@implementation MRTrack

//NSDictionary *DataCounters()
+ (NSDictionary *)trackDataBytes
{
    
    struct ifaddrs *addrs;
    const struct ifaddrs *cursor;
    
    u_int32_t WiFiSent = 0;
    u_int32_t WiFiReceived = 0;
    u_int32_t WWANSent = 0;
    u_int32_t WWANReceived = 0;
    
    if (getifaddrs(&addrs) == 0)
    {
        cursor = addrs;
        while (cursor != NULL)
        {
            if (cursor->ifa_addr->sa_family == AF_LINK)
            {
#ifdef DEBUG
                const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data;
                if(ifa_data != NULL)
                {
                    NSLog(@"Interface name %s: sent %tu received %tu",cursor->ifa_name,ifa_data->ifi_obytes,ifa_data->ifi_ibytes);
                }
#endif
                
                // name of interfaces:
                // en0 is WiFi
                // pdp_ip0 is WWAN
                NSString *name = [NSString stringWithFormat:@"%s",cursor->ifa_name];
                if ([name hasPrefix:@"en"])
                {
                    const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data;
                    if(ifa_data != NULL)
                    {
                        WiFiSent += ifa_data->ifi_obytes;
                        WiFiReceived += ifa_data->ifi_ibytes;
                    }
                }
                
                if ([name hasPrefix:@"pdp_ip"])
                {
                    const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data;
                    if(ifa_data != NULL)
                    {
                        WWANSent += ifa_data->ifi_obytes;
                        WWANReceived += ifa_data->ifi_ibytes;
                    }
                }
            }
            
            cursor = cursor->ifa_next;
        }
        
        freeifaddrs(addrs);
    }
    
    return @{DataCounterKeyWiFiSent:[NSNumber numberWithUnsignedInt:WiFiSent],
             DataCounterKeyWiFiReceived:[NSNumber numberWithUnsignedInt:WiFiReceived],
             DataCounterKeyWWANSent:[NSNumber numberWithUnsignedInt:WWANSent],
             DataCounterKeyWWANReceived:[NSNumber numberWithUnsignedInt:WWANReceived]};
}
@end

相关文章

  • iOS 统计手机流量

  • IOS流量统计

    最近做视频遇到了一个需求就是将流量放在视频上方展示 于是2开始各种百度最后终于解决直接上代码吧 #include....

  • 统计手机用户流量日志

    统计手机用户流量日志需求分析 需要统计手机用户流量日志,日志内容实例: 要把同一个用户的上行流量、下行流量进行累加...

  • iOS 网络流量统计

    iOS 网络流量统计 计算项目中流量的发送、接受和消耗 - (NSDictionary*)getTrafficMo...

  • hadoop入门-MapReduce实例(二)

    本次尝试自定义输出类型手机流量分为上传流量和下载流量,统计的时候需要得到的结果表示为(手机号 上传流量 下载流量 ...

  • MapReduce项目实战(流量统计项目)

    经典案例:流量统计 1、需求一:统计每个手机号的数据包和流量总和 2、需求二:将需求一中结果按照upFlow流量倒...

  • 普通Http接口 & WKWebview & U

    1 统计 普通http 接口 流量2 统计 UIWebView 流量3 统计 WKWebView 流量 (需要 调...

  • iOS 统计流量信息(转)

    转自:http://www.cnblogs.com/jukaiit/p/5730228.html 在开发中,有时候...

  • 《iOS开发进阶》--读书笔记

    《iOS开发进阶》作者:唐巧 统计分析工具 Flurry: 国外软件,专门针对移动的做了优化,统计流量小,数据安全...

  • 流量统计

    参考 https://blog.csdn.net/w7849516230/article/details/7170...

网友评论

      本文标题:iOS 统计手机流量

      本文链接:https://www.haomeiwen.com/subject/nedtextx.html