runtime获取当前状态栏网络连接

状态栏由控制器控制的, 首先获取当前app
UIApplication * app = [UIApplication sharedApplication];

遍历当前app的所有属性

//    unsigned int outCount = 0;
//    id statusBar = [app valueForKeyPath:@"statusBar"];
//    Ivar *ivars = class_copyIvarList([statusBar class], &outCount);
//
//    for (int i = 0; i < outCount; i++) {
//        
//         Ivar ivar = ivars[i];
//         printf("aaa = |%s i = %d\n", ivar_getName(ivar), i);
////        NSLog(@"i = %d",i);
//    }
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    int type = 0;
    for (id child in children) {
//        NSLog(@"child = %@", [child class]);
//        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//                        // 遍历当前状态栏的所有属性,找到关于状态栏的
//            unsigned int outCount = 0;
//            Ivar *ivars = class_copyIvarList([child class], &outCount);
//
//            for (int i = 0; i < outCount; i++) {
//                     Ivar ivar = ivars[i];
//                    printf("i= %d |%s\n", i, ivar_getName(ivar));
//                }
//            }
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];              
      }       
}
NSLog(@"----%d", type);

你可能感兴趣的:(runtime获取当前状态栏网络连接)