Tweak-Tutorial

How Do You Create a Tweak?

What is MSHookIvar?

MSHookIvar is a function present in the Cydia Substrate API, used to obtain instance variables (ivars) from objects. It can only be used in .xm files, so if it is needed to be used, the file should be renamed to have a .xm extension.

What is its syntax?

MSHookIvar<type>(object, name_of_ivar);

Example use-case

#import <UIKit/UIKit.h>

%hook PSTableCell

-(void)didMoveToWindow {
    UITableViewLabel *detail = MSHookIvar<UITableViewLabel *>(self, "_detailTextLabel");
    detail.textColor = UIColor.redColor;
    %orig;
}

%end

Alternative to MSHookIvar

-valueForKey: is an alternative to MSHookIvar when it cannot be used. It is also not a part of the Cydia Substrate API. It will work most of the time and is also useful for when a file cannot have a .xm extension.

Its syntax looks like this:

[object valueForKey:name_of_ivar];

In addition, there is also -safeValueForKey:, which is a private method that is present in the NSObject class. The difference between -valueForKey: and -safeValueForKey: is that -valueForKey: will crash if the instance variable is not present, while -safeValueForKey: will just return a nil value and not crash.

Another Example use-case

#import <UIKit/UIKit.h>

%hook PSTableCell

-(void)didMoveToWindow {
    UITableViewLabel *detail = [self valueForKey:@"_detailTextLabel"];
    detail.textColor = UIColor.redColor;

    %orig;
}

%end

Previous Page (Preference Bundles cont.)

Next Page (Avoiding layoutSubviews)