iPhone数据使用跟踪/监控 [英] iPhone Data Usage Tracking/Monitoring

查看:107
本文介绍了iPhone数据使用跟踪/监控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个主题,但发现很少有些有用的细节。根据这些细节,我尝试按如下方式编写一些代码。



注意:请将此帖中分享的详细信息与其他帖子进行比较,然后再将其标记为DUPLICATE ,而不仅仅是主题。

   - (NSArray *)getDataCountersForType:(int)type {
BOOL成功;
struct ifaddrs * addrs = nil;
const struct ifaddrs * cursor = nil;
const struct sockaddr_dl * dlAddr = nil;
const struct if_data * networkStatisc = nil;

int dataSent = 0;
int dataReceived = 0;

success = getifaddrs(& addrs)== 0;
if(success){
cursor = addrs;
while(cursor!= NULL){
if(cursor-> ifa_addr-> sa_family == AF_LINK){
dlAddr =(const struct sockaddr_dl *)cursor-> ifa_addr;
networkStatisc =(const struct if_data *)cursor-> ifa_data;

if(type == WiFi){
dataSent + = networkStatisc-> ifi_opackets;
dataReceived + = networkStatisc-> ifi_ipackets;
}
else if(type == WWAN){
dataSent + = networkStatisc-> ifi_obytes;
dataReceived + = networkStatisc-> ifi_ibytes;
}
}
cursor = cursor-> ifa_next;
}
freeifaddrs(addrs);
}
return [NSArray arrayWithObjects:[NSNumber numberWithInt:dataSent],[NSNumber numberWithInt:dataReceived],nil];
}

此代码收集iPhone设备的互联网使用信息(而不是我的应用程序)单独)。



现在,如果我通过WiFi或通过3G使用互联网,我只能在 ifi_obytes (已发送)和 ifi_ibytes (收到)但我认为我应该在 ifi_opackets 和<$ c $中获得WiFi使用c> ifi_ipackets



还想补充一点,如果我连接到WiFi网络,但我没有使用互联网,我仍然会得到值已添加到 ifi_obytes ifi_ibytes



可能是我在实施或理解上错了。需要有人帮助我。






编辑:而不是 AF_LINK 我尝试了 AF_INET sockaddr_in 而不是 sockaddr_dl )。这会使应用程序崩溃。

解决方案

问题是pdp_ip0是接口之一,所有pdpXXX都是专用于不同功能的WWAN接口,语音邮件,通用网络接口。



我在苹果论坛上读到:
操作系统不会逐个进行网络统计。因此,这个问题没有确切的解决方案。但是,您可以获取每个网络接口的网络统计信息。



通常,en0是您的Wi-Fi接口,pdp_ip0是您的WWAN接口。



因为特定的日期时间,没有好的方法来获取信息wifi /蜂窝网络数据!



数据统计(ifa​​_data-> ifi_obytes和ifa_data-> ifi_ibytes)存储在之前的设备重启中。



我不知道为什么,但ifi_opackets和ifi_ipackets只显示为lo0(我认为它的主界面)。



是的。然后设备通过WiFi连接,不使用互联网if_iobytes值仍然来,因为这种方法提供网络字节交换而不仅仅是互联网。

  #include< net / if.h> 
#include< ifaddrs.h>

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

NSDictionary * DataCounters()
{

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(@接口名称%s:发送%tu收到%tu,cursor-> ifa_name,ifa_data-> ifi_obytes,ifa_data - > ifi_ibytes);
}
#endif

//接口名称:
// en0是WiFi
// pdp_ip0是WWAN
NSString * name = @(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);
}

返回@ {DataCounterKeyWiFiSent:@(WiFiSent),
DataCounterKeyWiFiReceived:@(WiFiReceived),
DataCounterKeyWWANSent:@(WWANSent),
DataCounterKeyWWANReceived :@(WWANReceived)};
}

改进的复制/粘贴支持!


I've searched over this topic but found very few details which were helpful. With these details I've tried to cook some code as follows.

Note: Please compare the details shared in this post with other posts before marking this as DUPLICATE, and not just by the subject.

- (NSArray *)getDataCountersForType:(int)type {
    BOOL success;
    struct ifaddrs *addrs = nil;
    const struct ifaddrs *cursor = nil;
    const struct sockaddr_dl *dlAddr = nil;
    const struct if_data *networkStatisc = nil; 

    int dataSent = 0;
    int dataReceived = 0;

    success = getifaddrs(&addrs) == 0;
    if (success) {
        cursor = addrs;
        while (cursor != NULL) {
            if (cursor->ifa_addr->sa_family == AF_LINK) {
                dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
                networkStatisc = (const struct if_data *) cursor->ifa_data;

                if (type == WiFi) {
                    dataSent += networkStatisc->ifi_opackets;
                    dataReceived += networkStatisc->ifi_ipackets;   
                }
                else if (type == WWAN) {
                    dataSent += networkStatisc->ifi_obytes;
                    dataReceived += networkStatisc->ifi_ibytes; 
                }
            }
            cursor = cursor->ifa_next;
        }
        freeifaddrs(addrs);
    }       
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:dataSent], [NSNumber numberWithInt:dataReceived], nil];    
}

This code collects information of internet usage of an iPhone device (and not my application alone).

Now, if I use internet through WiFi or through 3G, I get the the data (bytes) only in ifi_obytes (sent) and ifi_ibytes (received) but I think I should get WiFi usage in ifi_opackets and ifi_ipackets.

Also wanted to add that if I'm connected to a WiFi network, but am not using internet, I still get value added to ifi_obytes and ifi_ibytes.

May be I'm wrong in the implementation or understanding. Need someone to help me out.


Edit: Instead of AF_LINK I tried AF_INET (sockaddr_in instead of sockaddr_dl). This crashes the application.

解决方案

The thing is that pdp_ip0 is one of interfaces, all pdpXXX are WWAN interfaces dedicated to different functions, voicemail, general networking interface.

i read in apple forum that : The OS does not keep network statistics on a process-by-process basis. As such, there's no exact solution to this problem. You can, however, get network statistics for each network interface.

In general en0 is your Wi-Fi interface and pdp_ip0 is your WWAN interface.

There is no good way to get information wifi/cellular network data since, particular date-time!

data statistic (ifa_data->ifi_obytes and ifa_data->ifi_ibytes) are stored from previous device reboot.

i don't know why, but ifi_opackets and ifi_ipackets are shown just for lo0 (i think its main interface ).

yes. Then device is connected via WiFi and doesn't use internet if_iobytes values still come because this method provides network bytes exchanges and not just internet .

#include <net/if.h>
#include <ifaddrs.h>

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

NSDictionary *DataCounters()
{

    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 = @(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 : @(WiFiSent),
             DataCounterKeyWiFiReceived : @(WiFiReceived),
             DataCounterKeyWWANSent : @(WWANSent),
             DataCounterKeyWWANReceived : @(WWANReceived)};
}

Improved copy/paste support !

这篇关于iPhone数据使用跟踪/监控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆