• iOS开发之获取WIFI信号强度


    虽然各种直接获取信号强度的api都被封杀了。但是还有一个另类的黑魔法可以获取到。那就是遍历UIStatusBar了

    - (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;
                }
            }
        }
    }
  • 相关阅读:
    mouseover 有一个多次触发的问题
    2019牛客多校第一场 H.XOR
    luoguP4570 [BJWC2011]元素(线性基)
    线性基
    2019牛客多校第七场 E.Find the median
    2019牛客多校第七场
    2019牛客多校第五场
    支配树
    2019牛客多校第四场
    Codeforces 1195E OpenStreetMap(单调队列)
  • 原文地址:https://www.cnblogs.com/starainDou/p/5781130.html
Copyright © 2020-2023  润新知