今天看了下TheOS的Logos,還有一個比較常用的標志就是%new,給hook的類中添加新的函數。比如,現在要給SpringBoard實現我某一個類的delegate方法。最簡單的就是讓SpringBoard實現alert的代理方法,使得點擊按鈕之后可以作出相應的響應。
第一步, 讓springBoard實現delegate方法。
#import <SpringBoard/SpringBoard.h>
@interface SpringBoard ()<UIAlertViewDelegate>
@end
這個作用大家都知道了,不多說了。
第二步就是添加alert的代理方法的實現到springboard中
%hook SpringBoard
%new(v@:@i)
- (void)alertView:(UIAlertView *)av clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(av.tag != 10010)
{
NSLog(@"av.tag != 10010");
return;
}
BOOL res = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://www.126.com"]];
if (res) {
NSLog(@"open myappTest://");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.126.com"]];
}
}
%end
這里重點說一下%new的用法。
一下摘自一個國外網站的回復
All Objective-C methods take at least two arguments, one of type 'id', the other of type 'SEL'. These arguments are 'implicit', in that they are not explicitly declared by the programmer. The first is named 'self' (the object receiving the message) and the other is '_cmd', the selector for the sent message.
@ = id
: = SEL
v@: = -/+ (void)method
v@:@ = -/+ (void)methodWithObjectid:(id)obj
v代表返回類型void @代表一個objc對象 :代表SEL;
i代表int類型
具體的符號代表什么還要參考objective-c運行時編程指南 (objective-c runtime Programming Guide)
上一個簡單demo的完整圖片,環境是用的iOSOpenDev
這個demo實現了,開機之后彈出一個對話框,點擊thanks之后會跳轉到126郵箱主頁。