1
BOOL   success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct if_data *networkStatisc;

float WiFiSent = 0;
float WiFiReceived = 0;
u_int64_t WWANSent = 0;
u_int64_t WWANReceived = 0;

NSString *name=[[[NSString alloc]init]autorelease];

success = getifaddrs(&addrs) == 0;
if (success)
{
    cursor = addrs;
    while (cursor != NULL)
    {
        name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
        NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name);
        // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN

        if (cursor->ifa_addr->sa_family == AF_LINK)
        {
            if ([name hasPrefix:@"en"])
            {
                //networkStatisc = (const struct if_data *) cursor->ifa_data;
                //WiFiSent+=networkStatisc->ifi_obytes;
                //WiFiReceived+=networkStatisc->ifi_ibytes;
                //NSLog(@"WiFiSent %f ==%d",WiFiSent,networkStatisc->ifi_obytes);
                //NSLog(@"WiFiReceived %f ==%d",WiFiReceived,networkStatisc->ifi_ibytes);
            }

            if ([name hasPrefix:@"pdp_ip"])
            {
                networkStatisc = (const struct if_data *) cursor->ifa_data;
                WWANSent+=networkStatisc->ifi_obytes;
                WWANReceived+=networkStatisc->ifi_ibytes;
                NSLog(@"WWANSent %llu ==%d",WWANSent,networkStatisc->ifi_obytes);
                NSLog(@"WWANReceived %llu ==%d",WWANReceived,networkStatisc->ifi_ibytes);
            }
        }

        cursor = cursor->ifa_next;
    }

    freeifaddrs(addrs);
}

i have commented the line which return the wifi uses data , but it still return the wifi data uses in WWAN interface.

I want to use only cellular data (3g data uses on iPhone) not wifi data.

Thanks

  • Are you not using Apple's [Reachability](https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html) or Tony Million's [Reachability](https://github.com/tonymillion/Reachability) Class? If you are, this should help you http://stackoverflow.com/questions/7938650/ios-detect-3g-or-wifi – Ajith Renjala Feb 22 '14 at 11:31

0 Answers0