There are several problems in getting mobile phone usage traffic:
①ios is unable to monitor the boot and shutdown broadcasts like Android, immediately shuts down and then turns on, and the traffic on the monitored mobile network is emptied by default.
② application in the background running, suddenly killed, unable to traffic statistics, need to be in the background when the application to save the flow of usage
③ If the user first opens the flow statistics software in February, when the 3rd month is opened, the traffic should be zero for February. But the problem is, what we can get is the total amount of traffic that is consumed from the start of the monitoring. Package flow for 1000M per month, if the February consumption of 200M traffic, March consumption of 300M traffic, we can not determine the March flow of surplus, we obtained the use of traffic for 500M. It is recommended that when the new one month begins, the user opens the app for the first time, reminding the user to reset the remaining traffic.
Note: Please test the code on the real machine
Core code:
HTTraffic.h
#import <Foundation/Foundation.h>
@interface httraffic:nsobject
+ (Nsdictionary *) gettrafficmonitorings;
@end
Httraffic.m
#import "HTTraffic.h" #include <arpa/inet.h> #include <net/if.h> #include <ifaddrs.h> #include < net/if_dl.h> @implementation httraffic/** * wifisent wifi send traffic * wifireceived WiFi receive traffic * wwansent mobile network Send traffic * Wwanreceived Mobile network Receive TRAFFIC * * + (Nsdictionary *) gettrafficmonitorings {nsdictionary * trafficdict = [[Nsdictionary alloc]
INIT];
BOOL success;
struct Ifaddrs *addrs;
const struct IFADDRS *cursor;
const struct If_data *networkstatisc;
int wifisent = 0;
int wifireceived = 0;
int wwansent = 0;
int wwanreceived = 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 consumption flow if ([name hasprefix:@ "en"]) {Networkstatisc = (const struct If_data *) cursor->ifa_data;
wifisent+=networkstatisc->ifi_obytes;
wifireceived+=networkstatisc->ifi_ibytes; }//mobile network consumption if ([name hasprefix:@ "Pdp_ip0"]) {Networkstatisc = (con
St struct If_data *) cursor->ifa_data;
wwansent+=networkstatisc->ifi_obytes;
wwanreceived+=networkstatisc->ifi_ibytes;
}} cursor = cursor->ifa_next;
} Freeifaddrs (Addrs);
} nsstring *wifisenttraffic = [NSString stringwithformat:@ "%d", wifisent];
NSString *wifireceivedtraffic = [NSString stringwithformat:@ "%d", wifireceived];
NSString *wifitotaltraffic = [NSString stringwithformat:@ "%d", Wifisent + wifireceived];
NSString *wwansenttraffic = [NSString stringwithformat:@ "%d", wwansent]; NSString *wwanreceivedtraffic = [nsstring stringwithformat:@ '%d ', wwanreceived];
NSString *wwantotaltraffic = [NSString stringwithformat:@ "%d", wwansent+wwanreceived]; Trafficdict = @{@ "wifisenttraffic": wifisenttraffic, @ "wifireceivedtraffic": Wifire Ceivedtraffic, @ "wifitotaltraffic": wifitotaltraffic, @ "wwansenttraffic": wwansenttr Affic, @ "wwanreceivedtraffic": wwanreceivedtraffic, @ "wwantotaltraffic": wwantotaltr
Affic};
return trafficdict; } @end
Traffic Monitoring Demo