雖然各類直接獲取信號強度的api都被封殺了。可是還有一個另類的黑魔法能夠獲取到。那就是遍歷UIStatusBar了api
- (void)getSignalStrength{ UIApplication *app = [UIApplication sharedApplication]; NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews]; NSString *dataNetworkItemView = nil; for (id subview in subviews) { if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) { dataNetworkItemView = subview; break; } } int signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue]; NSLog(@"signal %d", signalStrength); }
固然咱們還能夠遍歷UIStatusBar 獲取到網絡鏈接類型網絡
- (void)getNetworkType { UIApplication *app = [UIApplication sharedApplication]; NSArray *subviews = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews]; for (id subview in subviews) { if ([subview isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) { int networkType = [[subview valueForKeyPath:@"dataNetworkType"] intValue]; switch (networkType) { case 0: NSLog(@"NONE"); break; case 1: NSLog(@"2G"); break; case 2: NSLog(@"3G"); break; case 3: NSLog(@"4G"); break; case 5: { NSLog(@"WIFI"); } break; default: break; } } } }