0

我想知道如何确定我的设备是在 3G 还是 GPRS/4G 网络上。特别是它是否使用3G连接?有没有办法以编程方式做到这一点?

另外,我想以编程方式启用和禁用 3G?

即使建议使用私有 API 也可以。

4

2 回答 2

1

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.

于 2011-04-25T07:50:28.687 回答
1

如果您不介意使用私有 API(如果您想在 AppStore 上出售它,Apple 会拒绝),您可以使用下面的代码。然而,它在网络变化时有点不稳定。

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);
于 2013-01-21T06:47:57.630 回答