Tanu
Tanu

Reputation: 313

How to programmatically get whether iPhone uses 3G, 4G or GPRS connection? Is there a way to enable & disable 3G connection programmatically?

I wanted to know the way to decide whether my device is on 3G or GPRS/4G network. Especially whether it uses 3G connection or not? Is there any way to do it programmatically?

Also, I wanted to enable and disable 3G programmatically?

It will be fine even if private API is suggested.

Upvotes: 0

Views: 2165

Answers (2)

Shashank Patel
Shashank Patel

Reputation: 106

If you don't mind using private APIs (Apple will reject this if you want to sell it on AppStore), You may use the code bellow. However it is little unstable on network change.

void *libHandle=dlopen("/System/Library/PrivateFrameworks/SoftwareUpdateServices.framework/SoftwareUpdateServices",RTLD_LAZY);

Class SUNetworkMonitor = NSClassFromString(@"SUNetworkMonitor");
NSObject *networkMonitor=[SUNetworkMonitor sharedInstance];

// check if the class have the method currentNetworkType
if ( [networkMonitor respondsToSelector:@selector(currentNetworkType)] )
{
    int t = (int)[networkMonitor 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);
}
dlclose(libHandle);

Upvotes: 1

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

Well I can take the first part of the Question.

There is sample in iOS Developer Library - Reachability Take a look at Reachability.m it indicates whether you have a connection and the kind of connection.

Upvotes: 1

Related Questions