IOS实时监控上传下载速度

作者: Yes_Cui | 来源:发表于2016-10-28 09:40 被阅读490次

在开发中要获取网络类型是很简单的,导入Reachability直接获取状态就行了,现在我们要做一个类似下载器的那种实时把上传下载速度显示出来。

需要用到的头文件

使用Reachability

要测速度所以必须要有一个定时器,咱们为了不耗用户的流量,取的是数据的总量,然后减去上一次的检测的总量,得出的就是速度。网络现在分为wifi以及wwan两种类型。

首先头文件.h建立一个检测的数据类

@interface MonitorData : NSObject

@property (assign, nonatomic) float wwanSend;

@property (assign, nonatomic) float wwanReceived;

@property (assign, nonatomic) float wifiSend;

@property (assign, nonatomic) float wifiReceived;

@end

然后建立一个检测类

@interface MonitorFlow : NSObject

//开始检测

- (void)startMonitor;

//停止检测

- (void)stopMonitor;

@end

实现文件.M

//成员变量是内部可见的

@interface MonitorFlow ()

@property (strong,nonatomic) NSTimer *timer;

@property (assign, nonatomic) float tempWWANReceived;

@property (assign, nonatomic) float tempWWANSend;

@property (assign, nonatomic) float tempWifiReceived;

@property (assign, nonatomic) float tempWifiSend;

@end

直接把代码附上,里面有注释

@implementation MonitorFlow

- (void)startMonitor {

[self currentFlow];

self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(refreshFlow) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

}

- (void)stopMonitor{

[self.timer invalidate];

}

- (void)refreshFlow{

// 上传、下载

//不需要连通网络获取的是总的数据

Reachability *reachability = [Reachability reachabilityWithHostName:@"Yes-Cui"];

MonitorData *monitor = [self getMonitorDataDetail];

switch (reachability.currentReachabilityStatus) {

case ReachableViaWiFi:

{

float wifiSend = monitor.wifiSend - self.tempWifiSend;

float wifiReceived = monitor.wifiReceived - self.tempWifiReceived;

NSLog(@"wifi上传速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wifiSend]);

NSLog(@"wifi下载速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wifiReceived]);

}

break;

case ReachableViaWWAN:

{

float wwanSend = monitor.wwanSend - self.tempWWANReceived;

float wwanReceived = monitor.wifiReceived - self.tempWWANSend;

NSLog(@"wwan上传速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wwanSend]);

NSLog(@"wwan下载速度:%@",[NSString stringWithFormat:@"%.0f KB/s",wwanReceived]);

}

break;

default:

{

NSLog(@"无网络");

}

break;

}

[self currentFlow];

}

//赋值当前流量

- (void)currentFlow{

MonitorData *monitor = [self getMonitorDataDetail];

self.tempWifiSend = monitor.wifiSend;

self.tempWifiReceived = monitor.wifiReceived;

self.tempWWANSend = monitor.wwanSend;

self.tempWWANReceived = monitor.wwanReceived;

}

//上传、下载总额流量

- (MonitorData *)getMonitorDataDetail

{

BOOL success;

struct ifaddrs *addrs;

struct ifaddrs *cursor;

struct if_data *networkStatisc;

long tempWiFiSend = 0;

long tempWiFiReceived = 0;

long tempWWANSend = 0;

long tempWWANReceived = 0;

NSString *dataName;

success = getifaddrs(&addrs) == 0;

if (success)

{

cursor = addrs;

while (cursor != NULL)

{

dataName = [NSString stringWithFormat:@"%s",cursor->ifa_name];

if (cursor->ifa_addr->sa_family == AF_LINK)

{

if ([dataName hasPrefix:@"en"])

{

networkStatisc = (struct if_data *) cursor->ifa_data;

tempWiFiSend += networkStatisc->ifi_obytes;

tempWiFiReceived += networkStatisc->ifi_ibytes;

}

if ([dataName hasPrefix:@"pdp_ip"])

{

networkStatisc = (struct if_data *) cursor->ifa_data;

tempWWANSend += networkStatisc->ifi_obytes;

tempWWANReceived += networkStatisc->ifi_ibytes;

}

}

cursor = cursor->ifa_next;

}

freeifaddrs(addrs);

}

MonitorData *monitorData = [MonitorData new];

monitorData.wifiSend = tempWiFiSend/1024;

monitorData.wifiReceived = tempWiFiReceived/1024;

monitorData.wwanSend = tempWWANSend/1024;

monitorData.wwanReceived = tempWWANReceived/1024;

return monitorData;

}

@end

粘贴的格式错乱了自己排序一下,哈哈希望对大家有帮助。

网盘是有代码,要的拿走:

https://pan.baidu.com/s/1c1Xq5Ao

如需转载标明出处

各位看官觉得有用的话就请Yes-Cui喝瓶水吧,打赏一个

相关文章

  • IOS实时监控上传下载速度

    在开发中要获取网络类型是很简单的,导入Reachability直接获取状态就行了,现在我们要做一个类似下载器的那种...

  • iOS通过runloop监控卡顿

    质量监控-卡顿检测iOS实时卡顿监控基于Runloop简单监测iOS卡顿的demo微信iOS卡顿监控系统iOS-R...

  • ios 使用afn实时监控网络状态的变化

    ios 使用afn实时监控网络状态的变化 集成AFNetworking框架 #import AFNetwork...

  • 监听网络

    ios 注册通知、监听 iOS-OC-监听网络状态,有网时数据自动刷新 iOS实时监控网络状态的改变 简书 iOS...

  • [iOS] 线上UI卡顿监控与符号化速度优化

    本文参考以下文章,做了一点优化,提升了卡顿监测的准确性,性能,符号化速度等等。iOS实时卡顿监控,深入理解RunL...

  • ios实时网速监控

    思路:先获取到总的流量信息,然后用当前的总下行流量减去上一秒的下行流量得到网速!

  • iOS 性能优化-读书笔记

    1.微信读书 iOS 性能优化总结 2.iOS实时卡顿监控 3.获取iOS任意线程调用堆栈(五)完整实现:BSBa...

  • NetWorker for Mac 4.8.1 中文版下载 –

    NetWorker 是一款Mac上优秀的网络网速监控工具,可以实时监控当前的上传和下载速度,支持有线或无线,支持中...

  • CAT使用总结

    简介 CAT基于Java开发的实时应用监控平台,包括实时应用监控,业务监控。 CAT支持的监控消息类型包括: Tr...

  • ios实时的网络监控

    1:我们项目中经常会用到获取用户当前的网络状态,是否有网,是否是wifi情况下 准备工作 1:从苹果官网上下载Re...

网友评论

  • f30ae180496c:哥们,我想把你的改成实时监测网速要怎么做呢
    Yes_Cui:@念起_Knight 这个只能是发起一个网络请求,会消耗资源

本文标题:IOS实时监控上传下载速度

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