从状态栏崩溃中获取蜂窝信号

时间:2017-09-27 06:51:30

标签: ios11 xcode9 uistatusbar iphone-x

下面是我的代码,我正试图通过状态栏获得信号强度。

UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];

发生崩溃(iOS 11,Xcode 9,iPhone X): -

[UIStatusBar_Modern valueForUndefinedKey:]:此类不是密钥值编码兼容的关键前景视图

2 个答案:

答案 0 :(得分:6)

你可以用这个

NSArray *subviews = nil;
id statusBar = [[UIApplication sharedApplication] valueForKey:@"statusBar"];
if ([statusBar isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")]) {
    subviews = [[[statusBar valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
} else {
    subviews = [[statusBar valueForKey:@"foregroundView"] subviews];
}

答案 1 :(得分:0)

使用此:

- (NSString*)newtworkType {
    NSArray *subviews = nil;
    id statusBar = [[UIApplication sharedApplication] valueForKey:@"statusBar"];
    if ([statusBar isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")]) {
        subviews = [[[statusBar valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    } else {
        subviews = [[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:
            return @"No wifi or cellular";
            break;

        case 1:
            return @"2G";
            break;

        case 2:
            return @"3G";
            break;

        case 3:
            return @"4G";
            break;

        case 4:
            return @"LTE";
            break;

        case 5:
            return @"WIFI";
            break;


        default:
            break;

    }
    return @"";
}