博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于实时监测网络每秒上下行流量问题
阅读量:5148 次
发布时间:2019-06-13

本文共 5241 字,大约阅读时间需要 17 分钟。

今天接到该需求,上午google了许多,cocoachina了一些,结合了前人的工作,整合了一些思路,封装了一个类,

用户监测当前每秒实时网络流量、wifi信号强弱度0-3、网络环境。现把代码发布。

.h文件

////  NetworkStateOrRSSI.h//  GHZeusLibraries////  Created by 郭军 on 2017/12/2.//  Copyright © 2017年 ZJBL. All rights reserved.//#import 
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 
#include
#include
#import
#import
@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

你可能感兴趣的文章
你应该掌握的四种参数估计技术
查看>>
【计算机】DMA原理1
查看>>
百度前端学院-基础学院-第20到21天
查看>>
c#之冒泡排序的三种实现和性能分析
查看>>
订单删除,增加订单,巩固表单特定用法
查看>>
Linux命令之---nl
查看>>
nginx + uwsgi 跑python应用
查看>>
asp.net通过基类实现统一脚本和样式的管理
查看>>
『转』Bitdefender Internet Security 2013 – 免费1年
查看>>
pytorch搭建神经网络-第一篇博客
查看>>
Sublime Text 3 快捷键总结(拿走)
查看>>
return,break与continue的区别
查看>>
快排的递归和非递归C++
查看>>
微信公众平台开发(11) 发送客服消息
查看>>
MongoDB之$关键字及$修改器$set $inc $push $pull $pop
查看>>
关于对象
查看>>
CGo中传递多维数组给C函数
查看>>
android 调用系统照相机拍照后保存到系统相册,在系统图库中能看到
查看>>
ActionScript 3.0 宝典(中文PDF下载)
查看>>
Swift入门篇-集合
查看>>