上一篇文章: "静待花开:iOS密码学算法解读"
1.
如果遇到bool类型的方法,我们如何调试.
我依然用源项目代码做实验.
ViewController
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UIButton *revealBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:self.revealBtn];
[self booljudge];
}
-(BOOL)booljudge{
NSLog(@"hello bool judge");
int i = 100;
if (i > 0) {
NSLog(@"i > 0 ture");
return true;
}else{
NSLog(@"i < 0 false");
return false;
}
}
-(UIButton*)revealBtn{
if (!_revealBtn) {
_revealBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_revealBtn.frame = CGRectMake(100,100, 100,40);
_revealBtn.backgroundColor = [UIColor redColor];
[_revealBtn setTitle:@"测试" forState:UIControlStateNormal];
[_revealBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_revealBtn addTarget:self action:@selector(revealClick) forControlEvents:UIControlEventTouchUpInside];
}
return _revealBtn;
}
-(void)revealClick{
NSLog(@"hello");
}
@end
我在代码中加入了 -(BOOL)booljudge的方法
我们编译源码生成IPA文件后,从中提取可执行文件。
我们使用class-dump工具导出头文件。如果你是从这篇文章开始阅读的,你可以查看有关class-dump工具的详细介绍。
我们找到了一个名为ViewController的头文件。
#import "UIViewController.h"
@class UIButton;
@interface ViewController : UIViewController
{
UIButton *_revealBtn;
}
- (void).cxx_destruct;
- (_Bool)booljudge;
@property(retain, nonatomic) UIButton *revealBtn; // @synthesize revealBtn=_revealBtn;
- (void)revealClick;
- (void)viewDidLoad;
@end
我们找到
- (_Bool)booljudge
方法我们接着写hook方法的代码
-(id)booljudge{
NSLog(@"this a bool function");
return %orig;
}
这样只需使用return %orig来返回,以确保源码正常执行。
如果将返回类型修改为true或false,则只会执行一种逻辑。
我们接着对tweak工程进行编译到安装.如果你看到这里的朋友,你可以查看一下tweak工程文章,如何创建tweak项目.
下面是打印的日志输出
源代码是这样写的
-(BOOL)booljudge{
NSLog(@"hello bool judge");
int i = 100;
if (i > 0) {
NSLog(@"i > 0 ture");
return true;
}else{
NSLog(@"i < 0 false");
return false;
}
}
hook 代码
-(id)booljudge{
NSLog(@"this a bool function");
return %orig;
}
如果需要深入分析代码中的源码逻辑,最好的方式是亲自动手试试。通过实际操作和调试,您可以更好地理解代码的执行流程和逻辑结构,发现潜在的问题并进行解决。实践是加深理解和提升技能的最佳途径,因此鼓励大家积极动手,深入研究代码。
2.
有位小伙伴的cycript -p 命令执行不成功
- 有可能是系统的原因导致的.
我的手机系统是iOS12.3
cycript -p 应用名/进程名 出错了
我们该如何处理呢?
- 我们需要安装另一款软件.
1
wget http://apt.saurik.com/debs/cycript_0.9.594_iphoneos-arm.deb
2
wget http://www.tateu.net/repo/files/net.tateu.cycriptlistenertweak_1.0.0_iphoneos-arm.deb
3
wget http://www.tateu.net/repo/files/net.tateu.cyrun_1.0.5_iphoneos-arm.deb
4
pkg -i cycript_0.9.594_iphoneos-arm.deb
5
dpkg -i net.tateu.cycriptlistenertweak_1.0.0_iphoneos-arm.deb net.tateu.cyrun_1.0.5_iphoneos-arm.deb
- 我们安装完成后,我们可以这样执行命令
cyrun -n SpringBoard -e
Cycript加载到SpringBoard中。SpringBoard将终止并自动重启
cyrun -n SpringBoard -d
SpringBoard卸载Cycript。SpringBoard将终止并自动重启
- 我们来尝试一下
我们已经注入TEST项目
- 我们打印一下层级结构
UIApp.keyWindow.recursiveDescription().toString()
- 其他命令需要亲自尝试.
3.
在点击事件中加入其他控件
这里更简单了,因为在之前我写的hook demo里,我们已经加过一个alertView了。
%new
-(void)newMethod{
NSLog(@"first new mehtod");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"我的第一个tweak工程创建成功了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
[alertView show];
}
其他控件也可以同样的添加.
4.
总结
- 本篇文章主要介绍了如何解决三个问题:
当遇到bool类型的方法时,我们如何进行调试。
cycript -p 命令执行不成功的解决方法。
如何在点击事件中加入其他控件。
如果读者尝试了这些解决方案后仍然不适合自己的情况,可以考虑寻找其他原因,因为问题可能由多种因素引起,例如系统设置等。
感谢您的阅读和关注。