现在的位置: 首页 > 综合 > 正文

TheOS之%new的使用

2012年08月25日 ⁄ 综合 ⁄ 共 1372字 ⁄ 字号 评论关闭

    今天看了下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)

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html

 

上一个简单demo的完整图片,环境是用的iOSOpenDev

这个demo实现了,开机之后弹出一个对话框,点击thanks之后会跳转到126邮箱主页。

抱歉!评论已关闭.