In the development of use to get the speed of the place, there should be two kinds:
1, download speed, this can be directly in the receiving data of the local statistical calculation. That's not the story.
2, to obtain the data of the mobile card, you can monitor the network card flow, the following is.
#import "ViewController.h"
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <net/if.h>
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (getinternetface) userInfo : Nil Repeats:yes];
[Timer firedate];
}
-(void) Getinternetface {
Long Long hehe = [self getinterfacebytes];
NSLog (@ "Hehe:%lld", hehe);
}
/* Get network traffic information */
-(Long Long) getinterfacebytes
{
struct Ifaddrs *ifa_list = 0, *ifa;
if (Getifaddrs (&ifa_list) = =-1)
{
return 0;
}
uint32_t ibytes = 0;
uint32_t obytes = 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
/* Not a loopback device. */
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;
}
}
Freeifaddrs (ifa_list);
NSLog (@ "\n[getinterfacebytes-total]%d,%d", ibytes,obytes);
return ibytes + obytes;
}
@end
iOS development-real-time monitoring of speed