今天给大家带来一个实时监听网速的工具,不说废话,先看一下效果:
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
网友评论