iOS development monitors mobile phone traffic usage

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.