Hook to an instance method in iOS using theos and retrieve the argument that is being passed

吃可爱长大的小学妹 提交于 2019-12-11 16:56:39

问题


-(void)setID:(long long) is the method and I want retrieve the argument (the integer) being passed and show it in an alert view. I am new to this please help me. And also if possible, how to pass this argument to a different method.
-(void)setSelectedID:(long long), if this is the method I want to pass the arguments to, how would I do it in the Tweaks.xm file.
Any help would be appreciated, thanks.
Can this also be done using Cycript?


回答1:


this code is untested but I hope it can help (assuming that setSelectedID: is a method that you made) :

// Use parenthesis to avoid creating a duplicate definition of TheClassToHook
@interface TheClassToHook ()

// Put your new method definitions here

- (void)setSelectedID:(long long)passedID;

@end


%hook TheClassToHook

// This is the original method from TheClassToHook
- (void)setID:(long long)passedID {

    // Call your new method
    [self setSelectedID:passedID];

    // Create an NSString from the passed id,
    // it will be used to show it in the alert as a message
    NSString *msg = [NSString stringWithFormat:@"%lld", passedID];

    // Show an alert using UIAlertView, note that TheClassToHook should implement UIAlertViewDelegate
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The passed id is..."
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];

    [alert show];

    // Optional: Make sure the memory used to allocate the alert and its message are released,
    // this may be unnecessary
    [alert release];
    [msg release];

    /* NOTE:
        UIAlertView is deprecated since iOS 8,
        if you don't want to target older iOS versions,
        you should consider using UIAlertController instead
    */

    // Call the original method with the original arguments
    %orig;
}


// Use the "%new" logos directive to implement a new method
%new
- (void)setSelectedID:(long long)passedID {
    // Your code
}

%end // close %hook

If setSelectedID: is a method that is present by default, you can just remove its definition from the @interface block and its implementation in the %hook block.

Also, since I don't use Cycript, I don't know if it can be done using it, sorry.



来源:https://stackoverflow.com/questions/49251124/hook-to-an-instance-method-in-ios-using-theos-and-retrieve-the-argument-that-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!