背景: 接觸到iOS 逆天的注入式框架 FLEX,有諸多功能,包括查看控件的坐標(biāo)和屬性裸准,查看任何一個(gè)對象的屬性和成員變量脚乡,動(dòng)態(tài)修改屬性和成員變量,動(dòng)態(tài)的調(diào)用實(shí)例和類方法等等寄雀。為了方便隨時(shí)隨地的調(diào)用而不影響業(yè)務(wù)的邏輯功能得滤,就打算將調(diào)用位置設(shè)在狀態(tài)欄。
注意:在iOS13之后盒犹,狀態(tài)欄的管理全都由 UIStatusBarManager
這個(gè)類管理懂更。
代碼實(shí)現(xiàn)如下:
- (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];
}