ios 截取手机流量_iOS 统计手机流量

//

// MRTrack.h

// MRTrack

//

// Created by MccReeG on 2017/9/21.

// Copyright © 2017年 MccReeG. All rights reserved.

//

#import

@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

#include

#include

#include

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值