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