Hooking Instance Variables: Difference between revisions

From iPhone Development Wiki
(Created the page)
 
(Prototype, example and rewording)
Line 1: Line 1:
This page is dedicated to the use of the MSHookIvar function, included in Mobile Substrate, to modify instance variables.
This page is dedicated to the use of the MSHookIvar function, included in Mobile Substrate, to modify instance variables. This function is meant to be used when one can't get an instance variable through instance methods.


== Header definition ==


== Usage ==
<source lang="cpp">
template <typename Type_>
static inline Type_ &MSHookIvar(id self, const char *name);
</source>


The code works in the following format:<br />
Used like this:
<code>MSHookIvar<Class*>(varForIvar, "ivar_name")</code>


A number of things need to be noted:
<code>type ivar = MSHookIvar<type>(object, ivar_name);</code>
    1. The class can be any sort of class: C class (int, char, etc.), or any other Objective-C Class (CGRect, UILabel*, etc.)
 
    2. The varForIvar is commonly the variable that contains the instance variable you are hooking
Where:
    3. IMPORTANT: the ivar_name is a '''C String''', not an Objective-C String (so no @ sign)
 
# <tt>type</tt> is a primitive type (int, char, struct CGRect, a pointer or a reference to), a C++ class or an Objective-C class (NSObject *, UILabel *).
# <tt>object</tt> is the variable that contains the target instance variable.
# <tt>ivar_name</tt> is a '''C string''', not an Objective-C string (so no @ sign).
 
== Example usage ==
 
<source lang="logos">
// Given a private class
@interface SBIconController : NSObject {
SBIconContentView *_contentView;
}
+ (id)sharedInstance;
@end
 
// In some block of code:
{
...
SBIconController *iconController = [%c(SBIconController) sharedInstance];
SBIconContentView *contentView = MSHookIvar<SBIconContentView *>(iconController, "_contentView");
// Would return the same as [[%c(SBIconController) sharedInstance] contentView];
...
}
</source>

Revision as of 17:57, 28 March 2015

This page is dedicated to the use of the MSHookIvar function, included in Mobile Substrate, to modify instance variables. This function is meant to be used when one can't get an instance variable through instance methods.

Header definition

template <typename Type_>
static inline Type_ &MSHookIvar(id self, const char *name);

Used like this:

type ivar = MSHookIvar<type>(object, ivar_name);

Where:

  1. type is a primitive type (int, char, struct CGRect, a pointer or a reference to), a C++ class or an Objective-C class (NSObject *, UILabel *).
  2. object is the variable that contains the target instance variable.
  3. ivar_name is a C string, not an Objective-C string (so no @ sign).

Example usage

// Given a private class
@interface SBIconController : NSObject {
	SBIconContentView *_contentView;
}
+ (id)sharedInstance;
@end

// In some block of code:
{
	...
	SBIconController *iconController = [%c(SBIconController) sharedInstance];
	SBIconContentView *contentView = MSHookIvar<SBIconContentView *>(iconController, "_contentView");
	// Would return the same as [[%c(SBIconController) sharedInstance] contentView];
	...
}