Search code examples
ioscocoa-touchwifi

iOS Get Link Speed (Router Speed Test)


I want to test speed of connected router(wifi modem) from iOS app.

I've found something here Get link speed programmatically? but could not found sockios.h and ethtool.h

Is it possible to port this code to Objective-C or is there another way?

--

Sorry for the missing info and my poor english.

I want to test link speed (tx rate) between ios device and connected wifi modem.

There was a property named txRate in CWInterface class. I want to get that data in Cocoa Touch.

/*!
* @property
* @abstract Current transmit rate (Mbps) of the CoreWLAN interface. 
* @discussion Dynamically queries the interface for the current transmit rate.
*/
@property(readonly) NSNumber *txRate NS_DEPRECATED_MAC(10_6, 10_7);

Solution

  • Finally I've found the solution.

    #include <ifaddrs.h>
    #include <net/if.h>
    
    + (double)getRouterLinkSpeed
    {
        BOOL   success;
        struct ifaddrs *addrs;
        const struct ifaddrs *cursor;
        const struct if_data *networkStatisc;
    
        double linkSpeed = 0;
    
        NSString *name = [[NSString alloc] init];
    
        success = getifaddrs(&addrs) == 0;
        if (success)
        {
            cursor = addrs;
            while (cursor != NULL)
            {
                name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
    
                if (cursor->ifa_addr->sa_family == AF_LINK)
                {
                    if ([name hasPrefix:@"en"])
                    {
                        networkStatisc = (const struct if_data *) cursor->ifa_data;
                        linkSpeed = networkStatisc->ifi_baudrate;
                    }
                }
                cursor = cursor->ifa_next;
            }
            freeifaddrs(addrs);
        }
    
        return linkSpeed;
    }