以下是我的代码,我试图通过状态栏获得信号强度。
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
崩溃发生在(iOS 11,xcode 9,iPhone x): -
[uistatusbar_modern valueforundefinedkey:]:此类不适合键入键值编码
您可以使用此
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];
}
使用以下:
- (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 @"";
}