亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

iPhone數據使用跟蹤/監控

iPhone數據使用跟蹤/監控

繁星點點滴滴 2019-06-20 10:53:52
iPhone數據使用跟蹤/監控我已經搜索過這個話題,但是很少發現有幫助的細節。關于這些細節,我嘗試編寫如下代碼。注:請將此帖子中分享的細節與其他帖子進行比較,然后再將其標記為重復,而不僅僅是主題。- (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和ifi_ipackets.還想說,如果我連接到WiFi網絡,但不使用互聯網,我仍然可以獲得增值ifi_obytes和ifi_ibytes.也許我在實現或理解上錯了。需要有人幫我。編輯:而不是AF_LINK我試過AF_INET (sockaddr_in而不是sockaddr_dl)。這會使應用程序崩潰。
查看完整描述

3 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

問題是pdp_ip0是接口之一,所有pdpXXXWWAN接口專用于不同功能,語音信箱,一般網絡接口。

我在蘋果論壇上讀到:操作系統不以進程為基礎保存網絡統計數據。因此,這個問題沒有確切的解決辦法。但是,您可以獲取每個網絡接口的網絡統計信息。

總體而言en0是你的Wi-Fi界面和pdp_ip0是你的WWAN接口。

沒有好的方式來獲取信息無線/蜂窩網絡數據,因為,特別的日期-時間!

數據統計(ifa_data->ifi_obytesifa_data->ifi_ibytes)是從以前的設備重新啟動存儲的。

我不知道為什么,但是ifi_opacketsifi_ipackets只為lo0(我認為它的主要接口)。

是。然后通過WiFi也不使用互聯網if_iobytes值仍然存在,因為此方法提供網絡字節交換,而不僅僅是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)};}

改進的復制/粘貼支持!


查看完整回答
反對 回復 2019-06-20
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

重要的是要了解這些計數器是提供的從設備上一次啟動開始。

因此,為了有效地利用它們,您應該將設備的正常運行時間與每個示例一起使用(您可以使用mach_absolute_time()-見這,這個獲得更多信息)

一旦您有了計數器示例+正常運行時間,您就可以有更好的啟發式方法來使用數據.


查看完整回答
反對 回復 2019-06-20
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

要添加已接受的答案,重要的是要認識到接口顯示的數據量溢出并重新啟動于0之后4 GB,尤其是當您使用此代碼計算兩個讀數之間的差異時。這是因為ifi_obytesifi_ibytesuint_32它們的最大值是4294967295.

此外,我建議使用unsigned ints表示包含發送和接收的數據的變量。正規化ints有一個無符號整數的最大值的一半,所以當添加ifi_obytes它可能會導致溢出。

unsigned int sent = 0;sent += networkStatisc->ifi_obytes;


查看完整回答
反對 回復 2019-06-20
  • 3 回答
  • 0 關注
  • 796 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號