首页 > 解决方案 > 为什么我无法获取 UIStatusBarSignalStrengthItemView?

问题描述

为了获取iphone上的wifi或蜂窝信号强度,我编写了以下代码作为各个站点的参考,但它没有出现在“UIStatusBarSignalStrengthItemView”或“_UIStatusBarCellularSignalView”的IF语句中。

你需要许可吗?

■开发环境

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

■登出输出

10'; 用户交互启用 = 否;layer = <_UILabelLayer: 0x1d0281f40>>>\n)}>\";\n pill = \"<_UIStatusBarRegion: 0x1d013b8a0: identifier=pill, enabled=0, displayItems={(\n)}>\";\n systemUpdates = \"<_UIStatusBarRegion: 0x1d0

1

2

3

4

5

7

8

类 - UIView

啊啊啊

类 - UIView

啊啊啊

类 - UIView

啊啊啊

类 - UIView

啊啊啊

类 - UIView

啊啊啊

类 - UIView

啊啊啊

cccccc

cccccc

cccccc

cccccc

cccccc

cccccc

9

12345

23456

34567

45678

56789

56789

56789

56789

56789

56789

标签: iosobjective-cxcodesignal-strength

解决方案


虽然通过检查状态栏视图层次结构在某些早期版本的 iOS 中工作来获取信号强度,但从 iOS 11 开始不再工作。此外,依赖内部视图是(并且一直是)在应用商店审查中被拒绝的理由过程。

没有支持的方式来获取蜂窝信号强度。资料来源:苹果开发者论坛评论。获取 WiFi 信号强度的唯一支持方法是使用NEHotspotNetwork signalStrength属性。但是这个属性只对热点助手应用程序可用,并且需要一个特殊的权利


推荐阅读