程序猿阵线联盟-汇总各类技术干货iOS-Developer-SwiftiOS-Developer-OC

OC中实时监听网速的工具类

2018-08-28  本文已影响4人  JollyJerry

今天给大家带来一个实时监听网速的工具,不说废话,先看一下效果:

image.png

工具类中暴露出两个方法:
开始监听
- (void)startCheckNet;
结束监听
- (void)stopCheckNet;

暴露出两个通知,可以通过接收通知全局监听网络速度,也可以通过单例的成员变量监听网络速度:

方法一:通过NSNotification
// 监听下行速度
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkDownloadNetSpeed:) name:GJDownloadNetworkSpeedNotificationKey object:nil];
// 监听上行速度
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkUploadNetSpeed:) name:GJDownloadNetworkSpeedNotificationKey object:nil];



#pragma mark - 下行速度
-(void)checkDownloadNetSpeed:(NSNotification *)notification {
    
    self.downloadLabel.text = [NSString stringWithFormat:@"下行速度:%@ ",notification.object[@"NetSpeed"]];
    
    
}
#pragma mark - 上行速度
-(void)checkUploadNetSpeed:(NSNotification *)notification {

    self.uploadLabel.text = [NSString stringWithFormat:@"上行速度:%@ ",notification.object[@"NetSpeed"]];
}

方法二:通过访问成员变量

-------------------------------------下面是文件内容---------------------------------------

image.png

传送门:https://github.com/guojieios/NetSpeed.git

上一篇 下一篇

猜你喜欢

热点阅读