ios 网速监控_IOS_网速流量实时监控

时时网速监控,方便全局监控当前流量使用

import

// 88kB/s

extern NSString *const GSDownloadNetworkSpeedNotificationKey;

// 2MB/s

extern NSString *const GSUploadNetworkSpeedNotificationKey;

@interface GSNetworkSpeed : NSObject

@property (nonatomic, copy, readonly) NSString*downloadNetworkSpeed;

@property (nonatomic, copy, readonly) NSString *uploadNetworkSpeed;

+ (instancetype)shareNetworkSpeed;

- (void)start;

- (void)stop;

@end

#import "GSNetworkSpeed.h"

#include

#include

#include

#include

NSString* const GSDownloadNetworkSpeedNotificationKey = @"GSDownloadNetworkSpeedNotificationKey";

NSString* const GSUploadNetworkSpeedNotificationKey = @"GSUploadNetworkSpeedNotificationKey";

@interface GSNetworkSpeed () {

//总网速

uint32_t _iBytes;

uint32_t _oBytes;

uint32_t _allFlow;

//wifi网速

uint32_t _wifiIBytes;

uint32_t _wifiOBytes;

uint32_t _wifiFlow;

//3G网速

uint32_t _wwanIBytes;

uint32_t _wwanOBytes;

uint32_t _wwanFlow;

}

@property (nonatomic, strong) NSTimer* timer;

@end

@implementation GSNetworkSpeed

static GSNetworkSpeed* instance = nil;

+ (instancetype)shareNetworkSpeed

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

instance = [[self alloc] init];

});

return instance;

}

+ (instancetype)allocWithZone:(struct _NSZone*)zone

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

instance = [super allocWithZone:zone];

});

return instance;

}

- (instancetype)init

{

if (self = [super init]) {

_iBytes = _oBytes = _allFlow = _wifiIBytes = _wifiOBytes = _wifiFlow = _wwanIBytes = _wwanOBytes = _wwanFlow = 0;

}

return self;

}

#pragma mark - 开始监听网速

- (void)start

{

if (_timer == nil) {

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkNetworkSpeed) userInfo:nil repeats:YES];

[_timer fire];

}

}

//停止监听网速

- (void)stop

{

if ([_timer isValid]) {

[_timer invalidate];

_timer = nil;

}

}

- (NSString*)stringWithbytes:(int)bytes

{

if (bytes < 1024) // B

{

return [NSString stringWithFormat:@"%dB", bytes];

}

else if (bytes >= 1024 && bytes < 1024 * 1024) // KB

{

return [NSString stringWithFormat:@"%.1fKB", (double)bytes / 1024];

}

else if (bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024) // MB

{

return [NSString stringWithFormat:@"%.1fMB", (double)bytes / (1024 * 1024)];

}

else // GB

{

return [NSString stringWithFormat:@"%.1fGB", (double)bytes / (1024 * 1024 * 1024)];

}

}

- (void)checkNetworkSpeed

{

struct ifaddrs *ifa_list = 0, *ifa;

if (getifaddrs(&ifa_list) == -1) {

return;

}

uint32_t iBytes = 0;

uint32_t oBytes = 0;

uint32_t allFlow = 0;

uint32_t wifiIBytes = 0;

uint32_t wifiOBytes = 0;

uint32_t wifiFlow = 0;

uint32_t wwanIBytes = 0;

uint32_t wwanOBytes = 0;

uint32_t wwanFlow = 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;

// network

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;

allFlow = iBytes + oBytes;

}

//wifi

if (!strcmp(ifa->ifa_name, "en0")) {

struct if_data* if_data = (struct if_data*)ifa->ifa_data;

wifiIBytes += if_data->ifi_ibytes;

wifiOBytes += if_data->ifi_obytes;

wifiFlow = wifiIBytes + wifiOBytes;

}

//3G or gprs

if (!strcmp(ifa->ifa_name, "pdp_ip0")) {

struct if_data* if_data = (struct if_data*)ifa->ifa_data;

wwanIBytes += if_data->ifi_ibytes;

wwanOBytes += if_data->ifi_obytes;

wwanFlow = wwanIBytes + wwanOBytes;

}

}

freeifaddrs(ifa_list);

if (_iBytes != 0) {

_downloadNetworkSpeed = [[self stringWithbytes:iBytes - _iBytes] stringByAppendingString:@"/s"];

[[NSNotificationCenter defaultCenter] postNotificationName:GSDownloadNetworkSpeedNotificationKey object:nil];

NSLog(@"_downloadNetworkSpeed : %@",_downloadNetworkSpeed);

}

_iBytes = iBytes;

if (_oBytes != 0) {

_uploadNetworkSpeed = [[self stringWithbytes:oBytes - _oBytes] stringByAppendingString:@"/s"];

[[NSNotificationCenter defaultCenter] postNotificationName:GSUploadNetworkSpeedNotificationKey object:nil];

NSLog(@"_uploadNetworkSpeed :%@",_uploadNetworkSpeed);

}

_oBytes = oBytes;

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值