[Objective-C] Network type (WIFI, 3G, EDGE/GPRS)

Using private Framework, in particular SoftwareUpdateServices.frameworkyou can distinguish between carrier network, in particular, if your connection is 3G or GPRS/EDGE, or WiFi.

Private API headers

https://github.com/kennytm/iphone-private-frameworks
https://github.com/mattlawer/iOS5-Private-Frameworks

SUNetworkMonitor Class

http://www.albertopasca.it/dstore/SUNetworkMonitor.h

How to?

You need include your framework. Note that you cannot include it from Xcode, but need to be loaded via code.

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]// load framework dinamicall from bundle
NSBundle *b =
[NSBundle bundleWithPath:
@"/System/Library/PrivateFrameworks/SoftwareUpdateServices.framework"];

if ( [b load] )
{
// load Class from STRING
Class NetworkMonitor = NSClassFromString(@"SUNetworkMonitor");
// alloc class
if ( !_NetPointer ) _NetPointer = [[NetworkMonitor alloc] init];

// check if the class have the method currentNetworkType
if ( [_NetPointer respondsToSelector:@selector(currentNetworkType)] )
{
int t = (int)[_NetPointer performSelector:@selector(currentNetworkType)];

NSString *type = @"";
switch ( t ) {
case 0:  type = @"NO-DATA"; break;
case 1:  type = @"WIFI"; break;
case 2:  type = @"GPRS/EDGE"; break;
case 3:  type = @"3G"; break;
default: type = @"OTHERS"; break;
}

NSLog(@"Network type: %@", type);
}
}[/code]

You can use also dlopen to load dinamically libs or frameworks on your code, in this way:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]void *libHandle =
dlopen("/System/Library/PrivateFrameworks/SoftwareUpdateServices.framework/SUNetworkMonitor", RTLD_LAZY);
dlclose(libHandle);[/code]

NOTE:

This is a undocumented use of private API.
Your app will be rejected from App Store if use this way.
I suggest to use only for personal test or cydia store.

 

Rif: albertopasca.it
Rif: stackoverflow.com

Alberto Pasca