iOS development-real-time monitoring of speed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.