iOS 網(wǎng)絡流量統(tǒng)計

iOS 網(wǎng)絡流量統(tǒng)計

計算項目中流量的發(fā)送、接受和消耗

-? (NSDictionary*)getTrafficMonitorings

{

? ? NSDictionary * trafficDict = [[NSDictionary alloc] init];

? ? BOOLsuccess;

? ? structifaddrs*addrs;

? ? conststructifaddrs*cursor;

? ? conststructif_data*networkStatisc;

? ? intWiFiSent =0;

? ? intWiFiReceived =0;

? ? intWWANSent =0;

? ? intWWANReceived =0;

? ? NSString *name=[[NSString alloc]init];

? ? success =getifaddrs(&addrs) ==0;

? ? if(success) {

? ? ? ? cursor = addrs;

? ? ? ? while(cursor !=NULL) {

? ? ? ? ? ? name=[NSString stringWithFormat:@"%s",cursor->ifa_name];


? ? ? ? ? ? if(cursor->ifa_addr->sa_family==AF_LINK) {

? ? ? ? ? ? ? ? //wifi消耗流量

? ? ? ? ? ? ? ? if([namehasPrefix:@"en"]) {

? ? ? ? ? ? ? ? ? ? networkStatisc = (conststructif_data*) cursor->ifa_data;

? ? ? ? ? ? ? ? ? ? WiFiSent+=networkStatisc->ifi_obytes;

? ? ? ? ? ? ? ? ? ? WiFiReceived+=networkStatisc->ifi_ibytes;

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? //移動網(wǎng)絡消耗流量

? ? ? ? ? ? ? ? if([namehasPrefix:@"pdp_ip0"]) {

? ? ? ? ? ? ? ? ? ? networkStatisc = (conststructif_data*) cursor->ifa_data;

? ? ? ? ? ? ? ? ? ? WWANSent+=networkStatisc->ifi_obytes;

? ? ? ? ? ? ? ? ? ? WWANReceived+=networkStatisc->ifi_ibytes;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? cursor = cursor->ifa_next;

? ? ? ? }

? ? ? ? freeifaddrs(addrs);

? ? }

? ? NSString*WiFiSentTraffic = [NSStringstringWithFormat:@"%d",WiFiSent];

? ? NSString*WiFiReceivedTraffic = [NSStringstringWithFormat:@"%d",WiFiReceived];

? ? NSString*WiFiTotalTraffic = [NSStringstringWithFormat:@"%d",WiFiSent + WiFiReceived];

? ? NSString*WWANSentTraffic = [NSStringstringWithFormat:@"%d",WWANSent];

? ? NSString*WWANReceivedTraffic = [NSStringstringWithFormat:@"%d",WWANReceived];

? ? NSString*WWANTotalTraffic = [NSStringstringWithFormat:@"%d",WWANSent+WWANReceived];

? ? trafficDict =@{

? ? ? ? ? ? ? ? ? ? @"WiFiSentTraffic":WiFiSentTraffic,

? ? ? ? ? ? ? ? ? ? @"WiFiReceivedTraffic":WiFiReceivedTraffic,

? ? ? ? ? ? ? ? ? ? @"WiFiTotalTraffic":WiFiTotalTraffic,

? ? ? ? ? ? ? ? ? ? @"WWANSentTraffic":WWANSentTraffic,

? ? ? ? ? ? ? ? ? ? @"WWANReceivedTraffic":WWANReceivedTraffic,

? ? ? ? ? ? ? ? ? ? @"WWANTotalTraffic":WWANTotalTraffic

? ? ? ? ? ? ? ? ? ? };

? ? /*

?? ? WiFi狀態(tài)下發(fā)送流量

?? ? WiFi狀態(tài)下接收流量

?? ? WiFi狀態(tài)下消耗總流量

?? ? 移動網(wǎng)絡下發(fā)送流量

?? ? 移動網(wǎng)絡下接收流量

?? ? 移動網(wǎng)絡下消耗總流量

?? ? */

? ? returntrafficDict;

}

Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容