iOS+AOP+切片+运行时
有时候有这样的需求,对于非源码的库我们想要干涉原先的代码执行过程,常见的使用场景就是埋点,当然可以使用现成的第三方框架,aspect。
1、场景
想要在VC的viewWillAppear的执行开始和执行结束分别插入我们想要执行的代码
UIViewController+VCAOP.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (VCAOP)
@end
NS_ASSUME_NONNULL_END
UIViewController+VCAOP.m
#import "UIViewController+VCAOP.h"
#import <objc/runtime.h>
@implementation UIViewController (VCAOP)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
void(^aspect)(SEL,SEL) = ^(SEL swizsel,SEL origsel) {
Method origMethod = class_getInstanceMethod([UIViewController class], origsel);
Method swizMethod = class_getInstanceMethod([self class], swizsel);
//class_getInstanceMethod 返回类的实例方法
BOOL addMehtod = class_addMethod([UIViewController class], origsel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
//把新的方法加入到目标类中
if (addMehtod) {
class_replaceMethod([UIViewController class], swizsel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
}else {
method_exchangeImplementations(origMethod, swizMethod);
}
};
aspect(@selector(viewDidLoadA),@selector(viewDidLoad));
});
}
- (void)viewDidLoadA
{
[self before];
//这里不会无限循环的,因为已经被替换成viewDidLoad了
[self viewDidLoadA];
[self after];
}
- (void)before
{
NSLog(@"开始执行viewWillAppear");
}
- (void)after
{
NSLog(@"结束执行viewWillAppear");
}
@end
这样的话就实现了在viewDidLoad方法执行之前和之后添加想要的代码,但是对于类簇有个例外,所谓类簇见 下文
NSMutableArray+arrayAOP.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSMutableArray (arrayAOP)
@end
NS_ASSUME_NONNULL_END
NSMutableArray+arrayAOP.m
#import "NSMutableArray+arrayAOP.h"
#import <objc/runtime.h>
@implementation NSMutableArray (arrayAOP)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
void(^aspect)(SEL,SEL) = ^(SEL swizsel,SEL origsel) {
Method origMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), origsel);
Method swizMethod = class_getInstanceMethod([self class], swizsel);
BOOL addMehtod = class_addMethod([self class], swizsel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
if (addMehtod)
{
class_replaceMethod([self class], swizsel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
}
else
{
method_exchangeImplementations(origMethod, swizMethod);
}
};
SEL swizsel = @selector(addObjectA:);
SEL origsel = @selector(addObject:);
aspect(swizsel,origsel);
});
}
- (void)addObjectA:(id)anObject
{
if (anObject) {
[self addObjectA:anObject];
}else {
NSLog(@"数组插入nil,要崩溃了");
[self addObjectA:@"[nil]"];
NSLog(@"避开崩溃,数组插入nil");
}
}
@end
因为NSMutableArray类型,在实际实例化之后,就是私有类了,需要通过objc_getClass("__NSArrayM")来获取类