IOS實時監(jiān)控上傳下載速度

在開發(fā)中要獲取網(wǎng)絡(luò)類型是很簡單的猿诸,導入Reachability直接獲取狀態(tài)就行了,現(xiàn)在我們要做一個類似下載器的那種實時把上傳下載速度顯示出來。

需要用到的頭文件


使用Reachability

要測速度所以必須要有一個定時器窜觉,咱們?yōu)榱瞬缓挠脩舻牧髁抠鞔欤〉氖菙?shù)據(jù)的總量逗余,然后減去上一次的檢測的總量,得出的就是速度画拾。網(wǎng)絡(luò)現(xiàn)在分為wifi以及wwan兩種類型菜职。

首先頭文件.h建立一個檢測的數(shù)據(jù)類

@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

實現(xiàn)文件.M

//成員變量是內(nèi)部可見的

@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{

// 上傳嫡意、下載

//不需要連通網(wǎng)絡(luò)獲取的是總的數(shù)據(jù)

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(@"無網(wǎng)絡(luò)");

}

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

粘貼的格式錯亂了自己排序一下耸序,哈哈希望對大家有幫助坎怪。

網(wǎng)盤是有代碼搅窿,要的拿走:

https://pan.baidu.com/s/1c1Xq5Ao

如需轉(zhuǎn)載標明出處

各位看官覺得有用的話就請Yes-Cui喝瓶水吧疾棵,打賞一個

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末是尔,一起剝皮案震驚了整個濱河市拟枚,隨后出現(xiàn)的幾起案子众弓,更是在濱河造成了極大的恐慌,老刑警劉巖脚乡,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奶稠,死亡現(xiàn)場離奇詭異,居然都是意外死亡竹握,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜈项,“玉大人,你說我怎么就攤上這事侥衬。” “怎么了浇冰?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵肘习,是天一觀的道長漂佩。 經(jīng)常有香客問我罪塔,道長,這世上最難降的妖魔是什么征堪? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任佃蚜,我火速辦了婚禮,結(jié)果婚禮上熟尉,老公的妹妹穿的比我還像新娘洲脂。我一直安慰自己,他們只是感情好往果,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布棚放。 她就那樣靜靜地躺著,像睡著了一般飘蚯。 火紅的嫁衣襯著肌膚如雪局骤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天赘来,我揣著相機與錄音凯傲,去河邊找鬼。 笑死幌缝,一個胖子當著我的面吹牛涵卵,可吹牛的內(nèi)容都是我干的荒叼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼坏晦,長吁一口氣:“原來是場噩夢啊……” “哼嫁乘!你這毒婦竟也來了亦渗?” 一聲冷哼從身側(cè)響起汁尺,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎狼荞,沒想到半個月后帮碰,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡殉挽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年一死,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片投慈。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡伪煤,死狀恐怖抱既,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蝙砌,我是刑警寧澤跋理,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布前普,位于F島的核電站,受9級特大地震影響骡湖,放射性物質(zhì)發(fā)生泄漏峻厚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一辖试、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧罐孝,春花似錦肥缔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽炮车。三九已至,卻和暖如春纪隙,著一層夾襖步出監(jiān)牢的瞬間扛或,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工悲伶, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留麸锉,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓花沉,卻偏偏與公主長得像碱屁,于是被迫代替她去往敵國和親娩脾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容