How to get the current active network type (wifi or 3G) of iPhone, without using the Reachability class

StackOverflow https://stackoverflow.com/questions/20779077

  •  21-09-2022
  •  | 
  •  

Question

In my case, I can not use the Reachability class, because I am using iPhone in a wifi network which can not access the internet. It's just an intranet wifi.

That means, the Reachability class will return Not Reachable.

Was it helpful?

Solution

 -(void)newtworkType {

 NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil;

for (id subview in subviews) {
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        dataNetworkItemView = subview;
        break;
    }
}


switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
    case 0:
        NSLog(@"No wifi or cellular");
        break;

    case 1:
        NSLog(@"2G");
        break;

    case 2:
        NSLog(@"3G");
        break;

    case 3:
        NSLog(@"4G");
        break;

    case 4:
        NSLog(@"LTE");
        break;

    case 5:
        NSLog(@"Wifi");
        break;


    default:
        break;
}
}

OTHER TIPS

For your intranet wifi I think you can also use Reachability

You only need to change the host name to your intranet host.

Such as :

struct sockaddr_in ip4addr;
int s;
ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons(80);
inet_pton(AF_INET, "192.168.0.1", &ip4addr.sin_addr);

s = socket(PF_INET, SOCK_STREAM, 0);
bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr);

Reachability * reach = [Reachability reachabilityWithAddress:&ip4addr];

above code is wrote by faildrop , https://github.com/tonymillion/Reachability/issues/6

I do not test , but I think it can deal with your problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top