使用theos hook SBStatusBarManager实现点击状态栏弹窗的编译错误求助
报错原因及修复方案
错误1:UIAlertController方法调用错误
alertControllerWithTitle:message:preferredStyle: 是UIAlertController的类方法,不需要先通过alloc创建实例再调用,直接用类对象调用即可,你之前的写法将类方法当做实例方法调用,所以找不到对应选择器。
错误2:present方法调用对象错误
SBStatusBarManager 是状态栏管理类,并非UIViewController子类,本身不具备presentViewController:animated:completion:方法,需要获取当前系统的顶层视图控制器来执行弹窗操作。
修复后的完整代码
#include <UIKit/UIKit.h> %hook SBStatusBarManager -(void)handleStatusBarTapWithEvent:(id)arg1{ // 正确调用类方法创建UIAlertController UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"I hope this works!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; // 获取顶层视图控制器弹出提示框 UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController; while (topVC.presentedViewController) { topVC = topVC.presentedViewController; } [topVC presentViewController:alert animated:YES completion:nil]; %orig; } %end
补充说明
如果部分系统版本下keyWindow获取异常,可以替换为遍历[UIApplication sharedApplication].windows数组获取当前激活的窗口再取rootViewController即可。
内容的提问来源于stack exchange,提问作者wretchedbeaches
相关产品推荐
相关产品推荐

