寫(xiě)在前面
本文主要是總結(jié)一下在我們?nèi)粘m?xiàng)目中會(huì)用到的一些關(guān)于Runtime的相關(guān)API,便于以后查閱。
isMemberOfClass 和 isKindOfClass 區(qū)別
在正式總結(jié)Runtime下相關(guān)API之前,先看看isMemberOfClass 和 isKindOfClass的區(qū)別:
- (BOOL)isMemberOfClass:(Class)cls;
+ (BOOL)isMemberOfClass:(Class)cls;
- (BOOL)isKindOfClass:(Class)cls;
+ (BOOL)isKindOfClass:(Class)cls;
我們來(lái)看一下這幾個(gè)方法的底層實(shí)現(xiàn):
- (BOOL)isMemberOfClass:(Class)cls {
return [self class] == cls;
}
+ (BOOL)isMemberOfClass:(Class)cls {
return self->ISA() == cls;
}
- (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = [self class]; tcls; tcls = tcls->getSuperclass()) {
if (tcls == cls) return YES;
}
return NO;
}
+ (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = self->ISA(); tcls; tcls = tcls->getSuperclass()) {
if (tcls == cls) return YES;
}
return NO;
}
例1:
// Person 類(lèi)
@interface Person : NSObject
@end
@implementation Person
@end
// Student 類(lèi)
@interface Student : Person
@end
@implementation Student
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *student = [[Student alloc] init];
NSLog(@"1 - %d",[student isMemberOfClass:[Student class]]);
NSLog(@"2 - %d",[student isKindOfClass:[Student class]]);
NSLog(@"3 - %d",[student isMemberOfClass:[Person class]]);
NSLog(@"4 - %d",[student isKindOfClass:[Person class]]);
}
return 0;
}
打印結(jié)果:
2023-01-31 16:21:04.801267+0800 SuperDemo[32839:16964367] 1 - 1
2023-01-31 16:21:04.801310+0800 SuperDemo[32839:16964367] 2 - 1
2023-01-31 16:21:04.801347+0800 SuperDemo[32839:16964367] 3 - 0
2023-01-31 16:21:04.801382+0800 SuperDemo[32839:16964367] 4 - 1
例2:
// Person 類(lèi)
@interface Person : NSObject
@end
@implementation Person
@end
// Student 類(lèi)
@interface Student : Person
@end
@implementation Student
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *student = [[Student alloc] init];
NSLog(@"1 - %d",[Student isMemberOfClass:object_getClass([Student class])]);
NSLog(@"2 - %d",[Student isKindOfClass:object_getClass([Student class])]);
NSLog(@"3 - %d",[Student isMemberOfClass:object_getClass([Person class])]);
NSLog(@"4 - %d",[Student isKindOfClass:object_getClass([Person class])]);
}
return 0;
}
打印結(jié)果:
2023-01-31 16:54:18.561752+0800 SuperDemo[33188:16979322] 1 - 1
2023-01-31 16:54:18.562298+0800 SuperDemo[33188:16979322] 2 - 1
2023-01-31 16:54:18.562390+0800 SuperDemo[33188:16979322] 3 - 0
2023-01-31 16:54:18.562430+0800 SuperDemo[33188:16979322] 4 - 1
注意:
[Student isKindOfClass:[NSObject class]];結(jié)果返回的是:YES 因?yàn)镹SObject的isa指向的是自己。
Runtime下的一些常用API
類(lèi)
// 動(dòng)態(tài)創(chuàng)建一個(gè)類(lèi)(參數(shù):父類(lèi),類(lèi)名,額外的內(nèi)存空間)
Class objc_allocateClassPair(Class _Nullable __unsafe_unretained superclass, const char * _Nonnull name, size_t extraBytes)
// 注冊(cè)一個(gè)類(lèi)(要在類(lèi)注冊(cè)之前添加成員變量)
void objc_registerClassPair(Class _Nonnull __unsafe_unretained cls)
// 銷(xiāo)毀一個(gè)類(lèi)
void objc_disposeClassPair(Class _Nonnull __unsafe_unretained cls)
// 獲取isa指向的Class
Class object_getClass(id _Nullable obj)
// 設(shè)置isa指向的Class
Class object_setClass(id _Nullable obj, Class _Nonnull __unsafe_unretained cls)
// 判斷一個(gè)對(duì)象是否為Class
object_isClass(id _Nullable obj)
// 判斷一個(gè)類(lèi)是否為元類(lèi)
class_isMetaClass(Class _Nullable __unsafe_unretained cls)
// 獲取父類(lèi)
class_getSuperclass(Class _Nullable __unsafe_unretained cls)
成員變量
// 獲取一個(gè)實(shí)例變量
Ivar class_getInstanceVariable(Class _Nullable __unsafe_unretained cls, const char * _Nonnull name)
// 拷貝實(shí)例變量列表(最后需要調(diào)用free釋放)
Ivar *class_copyIvarList(Class _Nullable __unsafe_unretained cls, unsigned int * _Nullable outCount)
// 設(shè)置和獲取成員變量的值
void object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value)
id object_getIvar(id _Nullable obj, Ivar _Nonnull ivar)
// 動(dòng)態(tài)添加成員變量(已經(jīng)注冊(cè)的類(lèi)是不能添加成員變量的)
BOOL class_addIvar(Class _Nullable __unsafe_unretained cls, const char * _Nonnull name, size_t size, uint8_t alignment, const char * _Nullable types)
// 獲取成員變量的相關(guān)信息
const char *ivar_getName(Ivar _Nonnull v)
const char *ivar_getTypeEncoding(Ivar _Nonnull v)
方法
// 獲取一個(gè)實(shí)例方法、類(lèi)方法
Method class_getInstanceMethod(Class _Nullable __unsafe_unretained cls, SEL _Nonnull name)
Method class_getClassMethod(Class _Nullable __unsafe_unretained cls, SEL _Nonnull name)
// 方法實(shí)現(xiàn)相關(guān)操作
IMP class_getMethodImplementation(Class _Nullable __unsafe_unretained cls, SEL _Nonnull name)
IMP method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)
void method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
// 拷貝方法列表(最后需要通過(guò)free來(lái)釋放)
Method *class_copyMethodList(Class _Nullable __unsafe_unretained cls, unsigned int * _Nullable outCount)
// 動(dòng)態(tài)添加方法
BOOL class_addMethod(Class _Nullable __unsafe_unretained cls, SEL _Nonnull name, IMP _Nonnull imp, const char * _Nullable types)
// 動(dòng)態(tài)替換方法
IMP class_replaceMethod(Class _Nullable __unsafe_unretained cls, SEL _Nonnull name, IMP _Nonnull imp, const char * _Nullable types)
// 獲取方法的相關(guān)信息(帶copy的需要調(diào)用free去釋放)
SEL method_getName(Method _Nonnull m)
IMP method_getImplementation(Method _Nonnull m)
const char *method_getTypeEncoding(Method _Nonnull m)
unsigned int method_getNumberOfArguments(Method _Nonnull m)
char *method_copyReturnType(Method _Nonnull m)
char *method_copyArgumentType(Method _Nonnull m, unsigned int index)
// 選擇器相關(guān)
const char *sel_getName(SEL _Nonnull sel)
SEL sel_registerName(const char * _Nonnull str)
// 用block作為方法實(shí)現(xiàn)
IMP imp_implementationWithBlock(id _Nonnull block)
id imp_getBlock(IMP _Nonnull anImp)
BOOL imp_removeBlock(IMP _Nonnull anImp)
如何攔截按鈕的點(diǎn)擊事件
hook:可以理解為就是方法交換。
按鈕的點(diǎn)擊事件 會(huì)調(diào)用到UIControl的- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event這個(gè)方法。
攔截按鈕的點(diǎn)擊我們只需要攔截sendAction即可。
+ (void)load {
Method method1 = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method method2 = class_getInstanceMethod(self, @selector(ww_sendAction:to:forEvent:));
method_exchangeImplementations(method1, method2); // 調(diào)用這個(gè)方法之后會(huì)去清空之前的緩存
}
- (void)ww_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if ([self isKindOfClass:UIButton.class]) {
NSLog(@"我成功hook了按鈕...");
}
[self ww_sendAction:action to:target forEvent:event]; // 調(diào)用原來(lái)的sendAction
}
Runtime總結(jié)
Runtime在實(shí)際開(kāi)發(fā)中可能用到的場(chǎng)景:
- 利用關(guān)聯(lián)對(duì)象給分類(lèi)添加屬性
- 遍歷某個(gè)類(lèi)的所有成員變量(可以訪問(wèn)私有的成員變量 比如修改UITextField的占位label/ 字典轉(zhuǎn)模型/ 歸檔解檔)
- 交換方法實(shí)現(xiàn)(主要是交換系統(tǒng)的方法 利用方法交換做方法找不到導(dǎo)致的崩潰)
.........
寫(xiě)在最后
關(guān)于Runtime中常用的一些常用的API就總結(jié)到這里,如有錯(cuò)誤請(qǐng)多多指教。