Brandon Salgado
Brandon Salgado

Reputation: 51

How can I get UIButton to call target class and hook this method? (using theos to compile jailbreak tweak)

I am trying to put a UIButton into iPhones MobileSMS.app (messages app). It sucessfully appears in the view, but when you press it, it crashes of course because it is not calling any target class and hooking a method. The target class and method I would like to hook is in the second code below, how can I achieve calling this when the button is pressed? (my main goal is to put a button in the conversation view and when it is tapped it will force SMS instead of automatically using iMessage.)

#import <UIKit/UIKit.h>
#import <ChatKit/ChatKit.h>

@interface CKTranscriptCollectionViewController : UIViewController
@end

%hook CKTranscriptCollectionViewController
-(void)loadView {
   %orig;
   UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   [button setTitle:@"SMS" forState:UIControlStateNormal]; 
   button.frame = CGRectMake(0, 0, 50, 100);
   [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:button]; 
}

-(void)buttonPressed {
    NSLog(@"Button Pressed!");
}

%end

Class and method I would like to call when button is tapped(which belongs in the header "ChatKit/CKConversation.h"):

%hook CKConversation
-(BOOL)forceMMS {

    return TRUE;

}
%end

Upvotes: 2

Views: 1526

Answers (2)

iMokhles
iMokhles

Reputation: 219

Opsss with logos to define new action/method you have to write

%new before this action or method

and i advice you to create a private class to use this action like that

[self buttonPressed];

Private Class Should looks like

@interface CKTranscriptCollectionViewController (TWEAKNAME)
-(void)buttonPressed;
@end

so your code should looks like

#import <UIKit/UIKit.h>
#import <ChatKit/ChatKit.h>

@interface CKTranscriptCollectionViewController : UIViewController
@end

@interface CKTranscriptCollectionViewController (TWEAKNAME)
-(void)buttonPressed;
@end

%hook CKTranscriptCollectionViewController
-(void)loadView {
   %orig;
   UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   [button setTitle:@"SMS" forState:UIControlStateNormal]; 
   button.frame = CGRectMake(0, 0, 50, 100);
   [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:button]; 
}

%new
-(void)buttonPressed {
    NSLog(@"Button Pressed!");
}

%end

GoodLuck

Upvotes: 0

user1823693
user1823693

Reputation:

its crashing because its specting a parameter. Try changing the method definition to:

-(void)buttonPressed:(id)sender 

or change the target to:

[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 2

Related Questions