MSHookIvar is a function present in the MobileSubstrate API, used to obtain ivar values of an object or instance. 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.
MSHookIvar<type>(object, name_of_ivar);
type
- This is the type of the ivar, so if the ivar for example is a UIView, thenUIView *
would be put there.object
- This is where the ivar is located, so for example it could beself
or an instance of a class.name_of_ivar
- This is obviously the name of the ivar, it has to be written as a C string rather than an NSString, so no@
before the string.
#import <UIKit/UIKit.h>
%hook PSTableCell
-(void)didMoveToWindow {
UITableViewLabel *detail = MSHookIvar<UITableViewLabel *>(self, "_detailTextLabel");
detail.textColor = UIColor.redColor;
%orig;
}
%end
-valueForKey:
is an alternative to MSHookIvar
when it cannot be used. Its syntax looks like this:
[object valueForKey:name_of_ivar];
object
- This is where the ivar is located, so for example it could beself
or an instance of a class.name_of_ivar
- This is obviously the name of the ivar. InvalueForKey
it can be written as a NSString though. Bare in mind though that despiteMSHookIvar
"ugly" syntax, it tends to be more effective.
#import <UIKit/UIKit.h>
%hook PSTableCell
-(void)didMoveToWindow {
UITableViewLabel *detail = [self valueForKey:@"_detailTextLabel"];
detail.textColor = UIColor.redColor;
%orig;
}
%end