IOS实时监控上传下载速度
在开发中要获取网络类型是很简单的,导入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 = ;
long tempWiFiReceived = ;
long tempWWANSend = ;
long tempWWANReceived = ;
NSString *dataName;
success = getifaddrs(&addrs) == ;
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/;
monitorData.wifiReceived = tempWiFiReceived/;
monitorData.wwanSend = tempWWANSend/;
monitorData.wwanReceived = tempWWANReceived/;
return monitorData;
}
@end
來源:https://www.jianshu.com/p/42ab08c998ae
IOS实时监控上传下载速度的更多相关文章
- [Swift通天遁地]四、网络和线程-(9)上传图片并实时显示上传进度
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【iOS 使用github上传代码】详解
[iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...
- iOS上架ipa上传问题那些事
iOS上架ipa上传问题那些事 原文: http://www.jianshu.com/p/1e22543285c2 字数513 阅读312 评论0 喜欢1 通过xcode直接打包上传,不会提示你的ip ...
- 使用IPTABLES限制IP上传下载速度,如何用iptables限速?
怎样使用IPTABLES限制IP上传下载速度,如何用iptables限速?我们先来看范例: iptables限制某IP的上传速度为1000KB/秒(8Mbps,流入服务器带宽),即在此IP所在的服务器 ...
- iOS多图上传
iOS多图上传涉及到多线程问题,个人比较喜欢使用GCD操作,下边是最近写的一个多图上传代码,附带相关注释 __block BOOL allSucc = YES; __block int m = 0; ...
- iOS自动化打包上传的踩坑记
http://www.cocoachina.com/ios/20160624/16811.html 很久以前就看了很多关于iOS自动打包ipa的文章, 看着感觉很简单, 但是因为一直没有AppleDe ...
- IOS开发-图片上传
目前IOS端开发,图片上传到服务器分为两种,一种是直接上到服务器,一种是借助第三方储存(减少服务器压力). 一.直接上传到服务器 /** * 代码演示 */ //*******UIImagePNGRe ...
- iOS 七牛云上传并获取图片----【客户端】
最近做了七牛云存储的有关内容,涉及到与后台交互获取验证的token,无奈,后台自命清高,不与理会,没办法呀,于是自己搞呗.首先呢在在七牛上注册一个账号,然后呢添加一个存储空间这时候空间名 ...
- 【iOS】文件上传小记
iOS由该系统提供API可以实现可以实现文件的上传和下载,有两种方法来. NSURLConnection与NSURLSession. 当中NSURLConnection是使用非常久的的一种方式.NSU ...
随机推荐
- VS + Qt5Designer + Anaconda环境配置
最近打算做一个模型训练工具,从来都不喜欢做UI的我,最终把目光放在了QtDesigner上.配环境的过程中在网上翻阅了不少博客,但大多是pycharm或者是VScode,使用VS的似乎不多.所以打算记 ...
- js怎么限制文本框input只能输入数字
1.说明 本篇文章介绍怎么使用js限制文本框只能输入数字 2.HTML代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1 ...
- Win32对话框工程笔记
Main.cpp #include <Windows.h> #include "resource.h" INT_PTR CALLBACK dialogProc(HWND ...
- 面试算法爱好者书籍/OJ推荐
面试算法爱好者书籍/OJ推荐 这个书单也基本适用于准备面试. 一.教科书 基本上一般的算法课本介绍的范围都不会超出算法导论和算法引论的范围.读完这两本书,其它的算法课本大致翻翻也就知道是什么货色了. ...
- 6 Maven聚合与集成
Maven的聚合特性能够把项目的各个模块聚合在一起构件,而Maven的继承特性能够帮助抽取各个模块相同的依赖和插件等配置,简化POM的同时,还能促进各个模块配置的一致性. 1.聚合 为 ...
- Objective-c继承与组合
Objective-C 继承 继承:是一种XXX是XXX的关系.例如:学生是人,所以学生与人就是继承的关系. #import <Foundation/Foundation.h> @inte ...
- 剑指Offer:树的子结构【26】
剑指Offer:树的子结构[26] 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 解题思路 分为两步: 第一步:在树A中找到和树B的根节点的值一 ...
- rtmp搭建直播系统
开发环境 Ubuntu 14.04 server nginx-1.8.1 nginx-rtmp-module nginx的服务器的搭建 安装nginx的依赖库 sudo apt-get update ...
- Looksery Cup 2015 C. The Game Of Parity —— 博弈
题目链接:http://codeforces.com/problemset/problem/549/C C. The Game Of Parity time limit per test 1 seco ...
- html5--5-9 绘制扇形
html5--5-9 绘制扇形 学习要点 综合运用已经学过的知识绘制一个扇形 矩形的绘制方法 rect(x,y,w,h)创建一个矩形 strokeRect(x,y,w,hx,y,w,h) 绘制矩形(无 ...