码迷,mamicode.com
首页 > 移动开发 > 详细

IOS Hook 类成员变量

时间:2015-04-30 10:22:00      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

It seems that in your case you are trying to use an instance variable of the class you are hooking. Modifying the instance variable does not work that way in tweaks. You have to use MSHookIvar to ‘hook‘ an instance variable (aka ivar). Example:

[Tweak.xm/mm]

#import <substrate.h> // necessary
#import <Foundation/Foundation.h>

@interface TheClassYouAreHooking : NSObject {
    NSString *_exampleVariable;
}
- (void)doSomething;
@end

NSString *_exampleVariableHooked;

%hook TheClassYouAreHooking
- (void)doSomething 
{
    // ‘Hook‘ the variable

    exampleVariableHooked = MSHookIvar<NSString *>(self, "_exampleVariable");

    // The name of the hooked variable does not need to be the same

    exampleVariableHooked = @"Hello World";

    // You can do ANYTHING with the object Eg. [exampleVariableHooked release];

}
%end

  

MSHookIvar can also hook stuff like BOOLs and floats etc.

exampleVariableHooked = MSHookIvar<BOOL>(self, "_someBOOL");

  

Its declared in substrate.h so you need to import that otherwise you will not be able to compile your tweak. Also as a bonus tip, I‘m just reminding you that you have to put the identifier of the app/framework you‘re hooking in your tweakname.plist.

So after you ‘hook‘ the variable you can change it to suit your needs. Happy coding!

IOS Hook 类成员变量

标签:

原文地址:http://www.cnblogs.com/dependence/p/4468150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!