它是在Theos开发中容许hook代码很是的简单明了,它可以替换、修改、新增方法或者类。php
这种等级必须以%end结尾,而且也不存在函数ios
%group Groupname
复制代码
若是为了支持适配不一样系统的代码,能够经过如下方式c++
%group iOS8
%hook IOS8_SPECIFIC_CLASS
// your code here
%end // end hook
%end // end group ios8
%group iOS9
%hook IOS9_SPECIFIC_CLASS
// your code here
%end // end hook
%end // end group ios9
%ctor {
if (kCFCoreFoundationVersionNumber > 1200) {
%init(iOS9);
} else {
%init(iOS8);
}
}
复制代码
全部的hook都有隐藏了一个“_ungrouped”的隐式分组;它用来修饰类名objective-c
%hook Classname
复制代码
hook之间的代码是SBApplicationController具体的函数bash
%hook SBApplicationController
-(void)uninstallApplication:(SBApplication *)application {
NSLog(@"Hey, we're hooking uninstallApplication:!");
%orig; // Call the original implementation of this method
return;
}
%end
复制代码
给hook的类添加新方法;signature是OC类型的新方法名,必须在%hook块之间app
%new // 修饰新的方法
%new(signature) // 修饰新的方法signature
复制代码
%hook ClassName
%new
- (id)someValue {
return objc_getAssociatedObject(self, @selector(someValue));
}
%end
复制代码
经过运行时建立的子类,ivars不支持;使用%new修饰新增的方法,若是须要访问新类,必须经过%c
操做符获取,能够在%group块中iphone
%subclass MyObject : NSObject
- (id)init {
self = %orig;
[self setSomeValue:@"value"];
return self;
}
//the following two new methods act as `@property (nonatomic, retain) id someValue;`
%new
- (id)someValue {
return objc_getAssociatedObject(self, @selector(someValue));
}
%new
- (void)setSomeValue:(id)value {
objc_setAssociatedObject(self, @selector(someValue), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
%end
%ctor {
MyObject *myObject = [[%c(MyObject) alloc] init];
NSLog(@"myObject: %@", [myObject someValue]);
}
复制代码
%property (nonatomic|assign|retain|copy|weak|strong|getter|setter) Type name;
复制代码
添加新的属性在类中,必须在%hook
修饰或者%subclass
修饰的内部。函数
%end
复制代码
用来修饰group/hook/subclass
的块结束ui
这部分的指令再也不group/hook/subclass
的块内部。this
%config(Key=Value);
复制代码
%hookf(rtype, symbolName, args...) { … }
复制代码
// Given the function prototype
FILE *fopen(const char *path, const char *mode);
// The hook is thus made
%hookf(FILE *, fopen, const char *path, const char *mode) {
NSLog(@"Hey, we're hooking fopen to deny relative paths!");
if (path[0] != '/') {
return NULL;
}
return %orig; // Call the original implementation of this function
}
复制代码
CFBooleanRef (*orig_MGGetBoolAnswer)(CFStringRef);
CFBooleanRef fixed_MGGetBoolAnswer(CFStringRef string)
{
if (CFEqual(string, CFSTR("StarkCapability"))) {
return kCFBooleanTrue;
}
return orig_MGGetBoolAnswer(string);
}
%hookf(CFBooleanRef, "_MGGetBoolAnswer", CFStringRef string)
{
if (CFEqual(string, CFSTR("StarkCapability"))) {
return kCFBooleanTrue;
}
return %orig;
}
复制代码
%ctor { … }
复制代码
匿名构造方法
%dtor { … }
复制代码
匿名销毁构造方法
只在函数block中
%init;
%init([<class>=<expr>, …]);
%init(Group[, [+|-]<class>=<expr>, …]);
复制代码
初始化分组或默认分组
%hook SomeClass
-(id)init {
return %orig;
}
%end
%ctor {
%init(SomeClass=objc_getClass("class with spaces in the name"));
}
复制代码
%c([+|-]Class)
复制代码
等价于实例对象,或者类名
%orig
%orig(arg1, …)
复制代码
调用原始方法,不可以在%new里面,
%log;
%log([(<type>)<expr>, …]);
复制代码
打印日志
Extension | Process order |
---|---|
.x | will be processed by Logos, then preprocessed and compiled as objective-c. |
.xm | will be processed by Logos, then preprocessed and compiled as objective-c++. |
.xi | will be preprocessed as objective-c first, then Logos will process the result, and then it will be compiled. |
.xmi | will be preprocessed as objective-c++ first, then Logos will process the result, and then it will be compiled. |
xi或者xmi文件能够使用#define 宏
自动动帮你生成对应类的全部hook方法
$THEOS/bin/logify.pl ./SSDownloadAsset.h
复制代码