美文网首页
iOS 统计手机流量

iOS 统计手机流量

作者: MccReeee | 来源:发表于2017-09-21 17:50 被阅读41次
    //
    //  MRTrack.h
    //  MRTrack
    //
    //  Created by MccReeG on 2017/9/21.
    //  Copyright © 2017年 MccReeG. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface MRTrack : NSObject
    
    + (NSDictionary *)trackDataBytes;
    
    @end
    
    
    //
    //  MRTrack.m
    //  MRTrack
    //
    //  Created by MccReeG on 2017/9/21.
    //  Copyright © 2017年 MccReeG. All rights reserved.
    //
    
    #import "MRTrack.h"
    #include <arpa/inet.h>
    #include <net/if.h>
    #include <ifaddrs.h>
    #include <net/if_dl.h>
    
    static NSString *const DataCounterKeyWWANSent = @"WWANSent";
    static NSString *const DataCounterKeyWWANReceived = @"WWANReceived";
    static NSString *const DataCounterKeyWiFiSent = @"WiFiSent";
    static NSString *const DataCounterKeyWiFiReceived = @"WiFiReceived";
    
    @implementation MRTrack
    
    //NSDictionary *DataCounters()
    + (NSDictionary *)trackDataBytes
    {
        
        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 = [NSString stringWithFormat:@"%s",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:[NSNumber numberWithUnsignedInt:WiFiSent],
                 DataCounterKeyWiFiReceived:[NSNumber numberWithUnsignedInt:WiFiReceived],
                 DataCounterKeyWWANSent:[NSNumber numberWithUnsignedInt:WWANSent],
                 DataCounterKeyWWANReceived:[NSNumber numberWithUnsignedInt:WWANReceived]};
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS 统计手机流量

          本文链接:https://www.haomeiwen.com/subject/nedtextx.html