How can I differentiate between wifi network interfaces and 3g/lte ones?

Hi,


I want to create an app that monitors mobile data traffic on ios devices.


Is there a way I can get all network interfaces and differentiate between the wifi ones and mobile data ones?


Thank you,

Adina

Replies

I want to create an app that monitors mobile data traffic on ios devices.

This is unlikely to end well. For some background, check out this post and the thread it links to.

Is there a way I can get all network interfaces and differentiate between the wifi ones and mobile data ones?

You can get a list of network interfaces using

getifaddrs
. You can distinguish interface types using the
ifi_type
field but there are some serious caveats. See this post for more about those caveats and this post for some code snippets.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Use AFNetworking, and AFNetworkReachabilityManager.

Then just use the code below, when this class which is a AFHTTPSessionManager extension calls the networking layer to service an API call, these tasks will execute, and a global Notification event will occur which can inform any of your view controllers of the network status. All you really care about is if the network is Not Reachable, and you can only get the WWAN status as reachable, you can't determine if your on LTE or 3G.


[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)

{

switch (status)

{

case AFNetworkReachabilityStatusReachableViaWWAN:

[client connectionStatus];

break;

case AFNetworkReachabilityStatusReachableViaWiFi:

[client connectionStatus];

break;

case AFNetworkReachabilityStatusNotReachable:

[client connectionStatus];

break;

}

}];

[manager.reachabilityManager startMonitoring];


-(void)connectionStatus

{

BOOL connected = [AFNetworkReachabilityManager sharedManager].reachable;


dispatch_async(dispatch_get_main_queue(),^{

[[NSNotificationCenter defaultCenter] postNotificationName:kNETWORK_REFRESH object:[NSNumber numberWithBool:connected]];

});

}

Thank you Eskimo for your detailed answer.

I was wondering, in the store there are some apps that promise to monitor data traffic. What API do these apps use since they were approved on the store? From my understanting this means that those app aren't reliable?


I was thinking ...what If i create a VPN, can I monitor the data traffic in this way?


Thanks again,

Adina

Hei Murphy,


Thanks for your answer, but your solution is not what I wanted to do. I don't want to know if the network is reachable..I want to count how many bytes of data are consumed at device level, sort of data consumption monitor.


Thanks again for taking the time to answer

I was wondering, in the store there are some apps that promise to monitor data traffic. What API do these apps use since they were approved on the store?

I can’t comment on specific apps but my general experience is that these apps are based on the

if_data
stuff returned by
getifaddrs
, and thus don’t work reliably. You’d have to run the app and see whether its behaviour lives up to its marketing claims.

what If i create a VPN, can I monitor the data traffic in this way?

Yes and no. The best your VPN can do is claim the default route, which means you’ll see most traffic on the device. You won’t, however, see traffic that’s specifically bound to the WWAN interface, and that includes various system services (most notably push notifications) and some third-party apps.

As I said earlier, this whole effort is unlikely to end well. iOS simply does not provide public APIs that are equivalent to what the user sees in Settings. There’s only two good ways to get this info:

  • Be the operating system

  • Interact with carrier infrastructure that has access to this information from the ‘other end’

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"