TheOS之%new的使用


    今天看了下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郵箱主頁。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM