TheOS之%new的使用

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.