iPhone数据使用情况跟踪/监视

萨希尔·卡纳(Sahil Khanna)

我已经搜索了该主题,但是发现很少有有用的细节。有了这些细节,我尝试编写一些如下的代码。

注意:在将其标记为DUPLICATE之前,不仅要按主题,还请比较此帖子中与其他帖子共享的详细信息。

- (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];    
}

这段代码收集了iPhone设备(而不是我的应用程序)的互联网使用情况信息。

现在,如果我通过WiFi或3G使用互联网,则只能在ifi_obytes(发送)和ifi_ibytes(接收)中获得数据(字节),但我认为应该在ifi_opackets和中获得WiFi使用率ifi_ipackets

还想补充一点,如果我连接到WiFi网络,但不使用互联网,我仍然会为ifi_obytes增值ifi_ibytes

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


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

用户名

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

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

通常en0,您的Wi-Fi界面pdp_ip0就是您的WWAN界面。

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

数据统计信息(ifa_data->ifi_obytesifa_data->ifi_ibytes)是从以前的设备重新引导开始存储的。

我不知道为什么,但是ifi_opacketsifi_ipackets仅用于lo0(我认为它是主界面)。

是。然后,设备通过进行连接,WiFi并且不使用Internetif_iobytes值,因为这种方法提供了网络字节交换,而不仅仅是Internet,因此仍然会使用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)};
}

改进的复制/粘贴支持!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章