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


    在开发中要获取网络类型是很简单的,导入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://www.jianshu.com/p/42ab08c998ae

  • 相关阅读:
    bzoj 4897 天赋 有向图的矩阵数定理
    bzoj 4621 Tc605 思想+dp
    bzoj 4596 [Shoi2016]黑暗前的幻想乡 矩阵树定理+容斥
    bzoj 4455 [Zjoi2016]小星星 树形dp&容斥
    获取Android设备无线和以太网MAC地址
    Java WebSocket库:https://github.com/TooTallNate/Java-WebSocket
    UsbManager, UsbDevice的简单示例
    Android开发之开机自动启动应用
    使用Microsoft Office 2007将文档转换为PDF
    C/C++实现删除字符串的首尾空格
  • 原文地址:https://www.cnblogs.com/edensyd/p/9176409.html
Copyright © 2020-2023  润新知