runtime 大家都很熟悉,iOS的動態(tài)特性是靠runtime來維護(hù)的。那么通過runtime我們可以做什么呢?
AOP編程
AOP即面向切片編程,很著名的Aspects 就是基于runtime特性
防止Hook
一般動態(tài)注入的方式有framework 和 Dylib, 那么如何動態(tài)注入呢?
有一些強調(diào)安全的app,會做Hook防范,這里也是基于runtime的特性來的。
當(dāng)然還有著名的Facebook 的fishhook 是基于iOS編譯過程的來做的系統(tǒng)C函數(shù)的鉤子,這個在執(zhí)行時間上就要比runtime 更早的防范了。
本篇文章主要是說一下日常開發(fā)中hook的幾種方式。了解了大概的注入方式,也即可根據(jù)不同情況進(jìn)行對應(yīng)的防護(hù)
首先我們先來說一下分類的方式
一般系統(tǒng)自帶類
平時有些需求需要我們對已經(jīng)寫好的代碼進(jìn)行適當(dāng)?shù)男薷?,來完善業(yè)務(wù)需求如:解析url 但是我們不能每個地方都去更改一遍,這時候就適合做分類
demo中做了NSURL+correctUrl.h分類來解析中文。代碼如下:
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method URLWithStr = class_getClassMethod(self, @selector(URLWithString:));
Method HKURL = class_getClassMethod(self, @selector(HKURLWithStr:));
//交換
method_exchangeImplementations(URLWithStr, HKURL);
});
}
+(instancetype)HKURLWithStr:(NSString *)str{
//調(diào)用系統(tǒng)原來的方法
NSURL * url = [NSURL HKURLWithStr:str];
if (url == nil) {
str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
url = [NSURL HKURLWithStr:str];
return url;
}
因為是系統(tǒng)類分類所以這樣既可完成方法的交換,最后記得在新方法里面一定要執(zhí)行一下系統(tǒng)方法,否則會破壞系統(tǒng)方法功能。
類簇
對于NSArray、NSMutableArray、NSDictionary、NSMutableDictionary等類進(jìn)行swizzleMethod,不能直接使用類似NSClassFromString(@"NSMutableArray") 因為這些類簇類,其實是一種抽象工廠的設(shè)計模式。抽象工廠內(nèi)部有很多其它繼承自當(dāng)前類的子類,抽象工廠類會根據(jù)不同情況,創(chuàng)建不同的抽象對象來進(jìn)行使用。
class 的獲取一般使用 [self class] 或者 類似objc_getClass("__NSArrayM")取到真正的類。如下代碼:
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
id obj = [[self alloc]init];
[obj swizzleMethod:@selector(addObject:) withMethod:@selector(LFAddObject:)];
});
}
-(void)LFAddObject:(id)obj{
if (obj) {
[self LFAddObject:obj];
}else{
NSLog(@"添加元素為nil");
}
}
- (void)swizzleMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
//這里打印cls 可以看見是__NSArrayM 對于NSArray、NSMutableArray、NSDictionary、NSMutableDictionary等類進(jìn)行swizzleMethod,不能直接使用類似NSClassFromString(@"NSMutableArray") 因為這些類簇類,其實是一種抽象工廠的設(shè)計模式。抽象工廠內(nèi)部有很多其它繼承自當(dāng)前類的子類,抽象工廠類會根據(jù)不同情況,創(chuàng)建不同的抽象對象來進(jìn)行使用
//class 的獲取一般使用 [self class] 或者 類似objc_getClass("__NSArrayM")取到真正的類
Class cls = [self class];
// Class cls = objc_getClass("__NSArrayM");
Method originalMethod = class_getInstanceMethod(cls, origSelector);
Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
BOOL didAddMethod = class_addMethod(cls,
origSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(cls,
newSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
上述swizzleMethod方法是目前方法交換比較普遍的寫法,手懶的同學(xué)可以直接拷貝去用哈
自定義
replace方式
有時候我們要單獨封裝一個injectionCode ,這個時候就需要我們注意:上面說過,在新方法里面一定要執(zhí)行以下原方法,否則會破壞原方法的功能
但是在封裝的類中我們是沒有原函數(shù)的,所以要先保存一下原函數(shù)。
IMP (*old_tz)(id self,SEL _cmd);
Method ViewController_tz = class_getInstanceMethod(objc_getClass("ViewController"), @selector(tz));
old_tz = method_getImplementation(ViewController_tz);
將原方法保存在old_tz中,此時只需要將新函數(shù)替換掉原函數(shù)即可
class_replaceMethod(objc_getClass("ViewController"), @selector(tz), my_tz, "v@:");
class_replaceMethod 會在沒有函數(shù)的時候自己創(chuàng)建一個新的函數(shù)
執(zhí)行的時候如下
void my_tz(id self,SEL _cmd){
NSLog(@"截獲了跳轉(zhuǎn)事件");
old_tz(self,_cmd);
}
除了class_replaceMethod 還有一種方式即get/set方式
get/set方式
直接上代碼,這個也比較簡單。
+(void)getAndSetTohook{
Method ViewController_tz = class_getInstanceMethod(objc_getClass("ViewController"), @selector(tz));
old_tz = method_getImplementation(ViewController_tz);
method_setImplementation(ViewController_tz, (IMP)my_tz);
}
將原函數(shù)的指針,直接指向新函數(shù)
以上即為runtime Method Swizzling 的幾種常見方式,做防護(hù)的時候可從這幾個函數(shù)進(jìn)行考慮。
完整demo