Search code examples
iosobjective-creachability

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


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.


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;
    }
    }