关于实时监测网络每秒上下行流量问题

今天接到该需求,上午google了许多,cocoachina了一些,结合了前人的工作,整合了一些思路,封装了一个类,
用户监测当前每秒实时网络流量、wifi信号强弱度0-3、网络环境。现把代码发布。

.h文件

//
//  NetworkStateOrRSSI.h
//  GHZeusLibraries
//
//  Created by 郭军 on 2017/12/2.
//  Copyright © 2017年 ZJBL. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum {
    GHNetworkErrorType = 0,
    GHNetwork2GType = 1,
    GHNetwork3GType = 2,
    GHNetwork4GType = 3,
    GHNetworkWifiType = 5
}GHNetworkType;

@interface  NSObject (NetworkStateOrRSSI)

@property (assign, nonatomic) uint32_t  nowIBytes;  //当前每秒下行流量,KB
@property (assign, nonatomic) uint32_t  nowOBytes;  //当前每秒上行流量

+ (GHNetworkType)networkType;

+ (int)wifiStrengthBars;

//开始检测,需每秒调用一次该方法,使获得nowIBytes&nowOBytes
- (void)detectionBytes;


@end

 .m文件

//
//  NetworkStateOrRSSI.m
//  GHZeusLibraries
//
//  Created by 郭军 on 2017/12/2.
//  Copyright © 2017年 ZJBL. All rights reserved.
//

#import "NSObject+NetworkStateOrRSSI.h"
#include <ifaddrs.h>
#include <sys/socket.h>
#include <net/if.h>
#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@interface NSObject (bytes)
@property (assign,nonatomic) uint32_t historyIBytes;
@property (assign,nonatomic) uint32_t historyOBytes;
@property (assign,nonatomic) uint32_t oldIBytes;
@property (assign,nonatomic) uint32_t oldOBytes;
@property (assign,nonatomic) BOOL     isFirst;
@end

@implementation NSObject (bytes)

- (void)setHistoryIBytes:(uint32_t)historyIBytes
{
    objc_setAssociatedObject(self, @selector(historyIBytes), [NSNumber numberWithInt:historyIBytes], OBJC_ASSOCIATION_ASSIGN);
}

- (uint32_t)historyIBytes
{
    return [objc_getAssociatedObject(self, _cmd) intValue];
}

- (void)setHistoryOBytes:(uint32_t)historyOBytes
{
    objc_setAssociatedObject(self, @selector(historyOBytes), [NSNumber numberWithInt:historyOBytes], OBJC_ASSOCIATION_ASSIGN);
}

- (uint32_t)historyOBytes
{
    return [objc_getAssociatedObject(self, _cmd) intValue];
}

- (void)setOldIBytes:(uint32_t)oldIBytes
{
    objc_setAssociatedObject(self, @selector(oldIBytes), [NSNumber numberWithInt:oldIBytes], OBJC_ASSOCIATION_ASSIGN);
}

- (uint32_t)oldIBytes
{
    return [objc_getAssociatedObject(self, _cmd) intValue];
}

- (void)setOldOBytes:(uint32_t)oldOBytes
{
    objc_setAssociatedObject(self, @selector(oldOBytes), [NSNumber numberWithInt:oldOBytes], OBJC_ASSOCIATION_ASSIGN);
}

- (uint32_t)oldOBytes
{
    return [objc_getAssociatedObject(self, _cmd) intValue];
}

- (void)setIsFirst:(BOOL)isFirst
{
    objc_setAssociatedObject(self, @selector(isFirst), @(isFirst), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)isFirst
{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)getInterfaceBytes
{
    struct ifaddrs *ifa_list = 0, *ifa;
    if (getifaddrs(&ifa_list) == -1)
    {
        return;
    }
    
    uint32_t iBytes = 0;
    uint32_t oBytes = 0;
    
    for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
    {
        if (AF_LINK != ifa->ifa_addr->sa_family)
            continue;
        
        if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))
            continue;
        
        if (ifa->ifa_data == 0)
            continue;
        
        /* Not a loopback device. */
        if (strncmp(ifa->ifa_name, "lo", 2))
        {
            struct if_data *if_data = (struct if_data *)ifa->ifa_data;
            
            iBytes += if_data->ifi_ibytes;
            oBytes += if_data->ifi_obytes;
        }
    }
    if (!self.isFirst) {
        self.historyIBytes = iBytes;
        self.historyOBytes = oBytes;
        self.isFirst=YES;
    }

    self.nowIBytes = (iBytes - self.historyIBytes)/1024 - self.oldIBytes;
    self.nowOBytes = (oBytes - self.historyOBytes)/1024 - self.oldIBytes;
    
    self.oldIBytes = (iBytes - self.historyIBytes)/1024;
    self.oldOBytes = (oBytes - self.historyOBytes)/1024;
    
    freeifaddrs(ifa_list);
}


@end

@implementation NSObject (NetworkStateOrRSSI)

- (void)setNowIBytes:(uint32_t)nowIBytes
{
    objc_setAssociatedObject(self, @selector(nowIBytes), [NSNumber numberWithInt:nowIBytes], OBJC_ASSOCIATION_ASSIGN);
}

- (uint32_t)nowIBytes
{
    return [objc_getAssociatedObject(self, _cmd) intValue];
}

- (void)setNowOBytes:(uint32_t)nowOBytes
{
    objc_setAssociatedObject(self, @selector(nowOBytes), [NSNumber numberWithInt:nowOBytes], OBJC_ASSOCIATION_ASSIGN);
}

- (uint32_t)nowOBytes
{
    return [objc_getAssociatedObject(self, _cmd) intValue];
}



+ (GHNetworkType)networkType
{
    UIApplication *app = [UIApplication sharedApplication];
    
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    
    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    return type;
}

+ (int)wifiStrengthBars
{
    UIApplication *app = [UIApplication sharedApplication];
    
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    
    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            type = [[child valueForKeyPath:@"wifiStrengthBars"] intValue];
        }
    }
    return type;
}

- (void)detectionBytes
{
    [self getInterfaceBytes];
}

@end
原文地址:https://www.cnblogs.com/GJ-ios/p/8010608.html