为了在iPhone上获得wifi或蜂窝信号强度, 我编写了以下代码,作为各种网站的参考, 但它不会出现在IF语句中 “ UIStatusBarSignalStrengthItemView”或“ _UIStatusBarCellularSignalView”。
您需要许可吗?
■开发环境
Xcode9.4.1
iPhoneX iOS:11.0.1
■Test1.m
#import "Test1.h"
@implementation Test1 : NSObject
- (int)antennaBar
{
NSLog(@"%@",[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] subviews]);
NSLog(@"antennaBar 1");
NSLog(@"antennaBar 2");
// NSArray *subviews = [[[apps valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSArray *subviews = nil;
NSLog(@"antennaBar 3");
id statusBar = [[UIApplication sharedApplication] valueForKey:@"statusBar"];
NSLog(@"antennaBar 4");
if ([statusBar isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")]) {
NSLog(@"antennaBar 5");
subviews = [[[statusBar valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
} else {
NSLog(@"antennaBar 6");
subviews = [[statusBar valueForKey:@"foregroundView"] subviews];
}
NSLog(@"antennaBar 7");
NSString *dataNetworkItemView = nil;
NSLog(@"antennaBar 8");
for (id subview in subviews) {
NSLog(@"Class - %@", NSStringFromClass([subview class]));
NSLog(@"antennaBar aaaaaaa");
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
NSLog(@"antennaBar bbbbbb");
dataNetworkItemView = subview;
break;
}
if([subview isKindOfClass:[NSClassFromString(@"_UIStatusBarCellularSignalView") class]]) {
NSLog(@"antennaBar eeeeee");
dataNetworkItemView = subview;
int fffff = [subview valueForKey:@"numberOfActiveBars"];
NSLog(@"fffff=%d",fffff);
break;
}
}
for (id subview in subviews)
{
NSLog(@"antennaBar cccccc");
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]])
{
NSLog(@"antennaBar dddddd");
[(NSNumber*)[subview valueForKey:@"dataNetworkType"] intValue];
}
}
NSLog(@"antennaBar 9");
NSLog(@"12345");
id statusBaraaaa = [[UIApplication sharedApplication] valueForKey:@"statusBar"];
NSArray *children = nil;
NSLog(@"23456");
children = [[[statusBaraaaa valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSLog(@"34567");
int type = 0;
NSLog(@"45678");
for (id child in children) {
NSLog(@"56789");
if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
NSLog(@"67890");
type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
}
}
if (type == 0) {
return NO;
}else{
return YES;
}
int aaa = [[dataNetworkItemView valueForKey:@"signalStrengthBars"] intValue];
NSLog(@"aaa=%d",aaa);
return aaa;
}
@end
■Test1.h
#import <Foundation/Foundation.h>
#import "UIKit/UIKit.h"
@interface Test1 : NSObject
- (int)antennaBar;
@end
■登出
( “ <_UIStatusBar:0x14e809020:区域= {\ n bottomLeading = \” <_ UIStatusBarRegion:0x1d013bd00:标识符= bottomLeading,已启用== 1,displayItems = {(\ n)}> \“; \ n ExpandedLeading = \” <_ UIStatusBarRegion:0x1d013bbc0 :identifier = expandedLeading,enabled = 0,displayItems = {(\ n)}> \“; \ n ExpandedTrailing = \” <_ UIStatusBarRegion:0x1d013bc60:identifier = expandedTrailing,enabled = 0,displayItems = {(\ n)}> \\ <; UI view = <_ UIStatusBarStringView:0x14e825ae0; frame =(14.3333 3.66667; 38.3333 18); text = '12:10'; userInteractionEnabled = NO; layer = <_UILabelLayer:0x1d0281f40 >>> \ n)}> \“; \ n药片= \“ <_ UIStatusBarRegion:0x1d013b8a0:标识符=药,已启用= 0,displayItems = {(\ n)}> \”; \ n systemUpdates = \“ <_ UIStatusBarRegion:0x 1d0
1
2
3
4
5
7
8
类-UIView
aaaaaaa
类-UIView
aaaaaaa
类-UIView
aaaaaaa
类-UIView
aaaaaaa
类-UIView
aaaaaaa
类-UIView
aaaaaaa
cccccc
cccccc
cccccc
cccccc
cccccc
cccccc
9
12345
23456
34567
45678
56789
56789
56789
56789
56789
56789
答案 0 :(得分:2)
虽然通过检查状态栏视图层次结构获得信号强度在某些早期版本的iOS中有效,但从iOS 11开始不再有效。 另外,依赖内部视图是(而且一直以来)在应用商店审查过程中被拒绝的基础。
没有获得蜂窝信号强度的受支持方法。资料来源:comment on Apple Developer Forum。 获得WiFi信号强度的唯一受支持的方法是使用NEHotspotNetwork signalStrength属性。 但是此属性仅可用于热点帮助程序和requires a special entitlement。
答案 1 :(得分:0)
您可以检索活动酒吧的数量,可能的酒吧数量以及用户是否连接到wifi或蜂窝电话。 (我相信这就是“模式”属性/ iVar的用途。)
我已使用inactive和activeColor方法根据信号强度设置wifi和蜂窝数据的颜色,而无需使用活动条形图,因为它是直观表示的。当然,我是在越狱设备上进行调整,并且使用此API并将其隐藏得不好会导致您的应用被拒绝。但无论如何,请尝试_UIStatusBarSignalView以获得所需的信息(当然,仅用于测试目的)。我认为这仅适用于现代状态栏/ iPhone X类型。
@interface _UIStatusBarSignalView : UIView <_UIStatusBarDisplayable> {
BOOL _smallSize;
BOOL _visible;
long long _numberOfBars;
long long _numberOfActiveBars;
long long _signalMode;
UIColor* _inactiveColor;
UIColor* _activeColor;
}
@property (assign,nonatomic) long long numberOfBars;
//@synthesize numberOfBars=_numberOfBars - In the implementation block
@property (assign,nonatomic) long long numberOfActiveBars;
/* You can use this compared to the output of numberOfBars property to get a
good idea of signal strength. */
//@synthesize numberOfActiveBars=_numberOfActiveBars - In the implementation
//block
@property (assign,nonatomic) long long signalMode;
//...(more to the interface)
@end
完整的接口可以在这里找到,即使类转储来自11.1.2,我也可以确认它在适用于iPhone X的iOS 12.4上正常工作。 http://developer.limneos.net/?ios=11.1.2&framework=UIKit.framework&header=_UIStatusBarSignalView.h