背景: 接触到iOS 逆天的注入式框架 FLEX,有诸多功能,包括查看控件的坐标和属性,查看任何一个对象的属性和成员变量,动态修改属性和成员变量,动态的调用实例和类方法等等。为了方便随时随地的调用而不影响业务的逻辑功能,就打算将调用位置设在状态栏。
注意:在iOS13之后,状态栏的管理全都由 UIStatusBarManager
这个类管理。
代码实现如下:
- (void)addStatusBarAction {
#if DEBUG
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
__weak __typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIStatusBarManager *statusBarmanager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
SEL sel = NSSelectorFromString(@"handleTapAction:");
[statusBarmanager aspect_hookSelector:sel withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, id tapObj) {
[weakSelf onClickStatusBar];
} error:nil];
});
} else {
__weak __typeof(self) weakSelf = self;
Class statusBar = NSClassFromString(@"UIStatusBar");
if (statusBar) {
SEL sel = NSSelectorFromString(@"touchesEnded:withEvent:");
[statusBar aspect_hookSelector:sel withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, NSSet *touches, UIEvent *event) {
[weakSelf onClickStatusBar];
} error:nil];
}
}
#endif
#endif
}
- (void)onClickStatusBar {
[[FLEXManager sharedManager] toggleExplorer];
}