主要有两个注意事项
1.判断方法是否被交换过,已经交换过就不再重复交换
if ([delegate respondsToSelector:@selector(secre_webview_webView:decidePolicyForNavigationAction:decisionHandler:)]) {
TLogD(@"WKNavigationDelegate class %@ has hooked yet.", [NSString stringWithUTF8String:object_getClassName(delegateClass)]);
return;
}
2.判断交换的对象为方法实现的类,不要在继承子类中进行方法交换。
原因:
2.1
父类superClass 中实现了methodA
子类selfClass 中没有实现methodA
2.2
如果selfClass中进入hook,那么实际交换的是selfClass中的新方法与superClass中的methodA; 这时selfClass已经hook,但是在superClass中找不过交换的新方法,会再次hook,最终进入死循环crash
2.3
所以,我们需要在selfClass中获取到实现methodA的superClass,直接交换superClass中的methodA,所有的子类方法都能调用到交换后的方法。
//直接交换实现方法的类,避免继承之间循环交换
//遍历父类找到实现方法的父类
Class c = [self enumerateClasses:delegateClass];
if (!c) {
//如果没有父类方法实现需要交换的方法,就默认交换自己类的方法
c = delegateClass;
}
[SECREMethodSwizzling swizzleMethod:@selector(webView:decidePolicyForNavigationAction:decisionHandler:) ofClass:c withMethod:@selector(secre_webview_webView:decidePolicyForNavigationAction:decisionHandler:) defaultMethod:@selector(secre_webview_default_webView:decidePolicyForNavigationAction:decisionHandler:) fromClass:WKWebView.class];
以webview为例代码实现
1.方法交换
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// swizzling method
[SECREMethodSwizzling swizzleMethod:@selector(setNavigationDelegate:) withMethod:@selector(secre_webview_setNavigationDelegate:) ofClass:WKWebView.class];
TLogD(@"has exchanged setNavigationDelegate: method.");
});
}
2.交换方法自己与需要添加处理的方法
- (void)secre_webview_setNavigationDelegate:(id<WKNavigationDelegate>)delegate {
if (delegate) {
// NSLog(@"Now is using %s as -[WKWebView setNavigationDelegate:]", __PRETTY_FUNCTION__);
[self secre_webview_swizzleMethodOfWKNavigationDelegate:delegate];
[self secre_webview_setNavigationDelegate:delegate];
}
}
- (void)secre_webview_swizzleMethodOfWKNavigationDelegate:(id<WKNavigationDelegate>)delegate {
Class delegateClass = delegate.class;
@synchronized (sc_hookLock) {
//直接交换实现方法的类,避免继承之间循环交换
Class c = [self enumerateClasses:delegateClass];
if (!c) {
c = delegateClass;
}
if ([delegate respondsToSelector:@selector(secre_webview_webView:decidePolicyForNavigationAction:decisionHandler:)]) {
TLogD(@"WKNavigationDelegate class %@ has hooked yet.", [NSString stringWithUTF8String:object_getClassName(delegateClass)]);
return;
}
Class wkWebViewClass = WKWebView.class;
// exchange implementation
[SECREMethodSwizzling swizzleMethod:@selector(webView:decidePolicyForNavigationAction:decisionHandler:) ofClass:c withMethod:@selector(secre_webview_webView:decidePolicyForNavigationAction:decisionHandler:) defaultMethod:@selector(secre_webview_default_webView:decidePolicyForNavigationAction:decisionHandler:) fromClass:wkWebViewClass];
// 将hook过的类添加进来
// [hookedClassNames addObject:NSStringFromClass(delegateClass)];
TLogD(@"WKNavigationDelegate class %@ has hooked.", [NSString stringWithUTF8String:object_getClassName(delegateClass)]);
}
}
#pragma mark - helper
//判断页面是否实现了某个sel
- (Class)enumerateClasses:(Class)delegateClass {
SEL sel = @selector(webView:decidePolicyForNavigationAction:decisionHandler:);
if ([self isContainSel:sel inClass:[delegateClass class]]) {
return delegateClass;
}
Class c = [delegateClass class];
while (c) {
c = class_getSuperclass(c);
if ([self isContainSel:sel inClass:[c class]]) {
return [c class];
}
}
return nil;
}
- (BOOL)isContainSel:(SEL)sel inClass:(Class)class {
unsigned int count;
Method *methodList = class_copyMethodList(class,&count);
for (int i = 0; i < count; i++) {
Method method = methodList[i];
NSString *tempMethodString = [NSString stringWithUTF8String:sel_getName(method_getName(method))];
if ([tempMethodString isEqualToString:NSStringFromSelector(sel)]) {
return YES;
}
}
return NO;
}
3.默认方法与自己需要添加的实现
#pragma mark - default Methods
- (void)secre_webview_default_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
decisionHandler(WKNavigationActionPolicyAllow);
}
#pragma mark - swizzling Methods
- (void)secre_webview_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
TLogD(@"%s: will decide whether can visit url: %@, webview url: %@", __PRETTY_FUNCTION__, navigationAction.request.URL, webView.URL);
NSLog(@"URL :%@", navigationAction.request.URL);
// do something
[self secre_webview_webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
}
网友评论