123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- //
- // BUSplashAdView+RQExtension.m
- // jiaPei
- //
- // Created by 张嵘 on 2021/4/6.
- // Copyright © 2021 JCZ. All rights reserved.
- //
- #import "BUSplashAdView+RQExtension.h"
- #import <objc/runtime.h>
- @implementation BUSplashAdView (RQExtension)
- #pragma mark - load
- + (void)load {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- Method oldMethod = class_getInstanceMethod([self class], @selector(setDelegate:));
- Method newMethod = class_getInstanceMethod([self class], @selector(hook_setDelegate:));
- method_exchangeImplementations(oldMethod, newMethod);
- });
- }
- #pragma mark - 交换代理方法
- - (void)hook_setDelegate:(id<BUSplashAdDelegate>)delegate {
- SEL oldSelector = @selector(splashAdWillVisible:);
- SEL newSelector = @selector(hook_splashAdWillVisible:);
- Method oldMethod_del = class_getInstanceMethod([delegate class], oldSelector);
- Method oldMethod_self = class_getInstanceMethod([self class], oldSelector);
- Method newMethod = class_getInstanceMethod([self class], newSelector);
- // 若未实现代理方法,则先添加代理方法
- BOOL isSuccess = class_addMethod([delegate class], oldSelector, class_getMethodImplementation([self class], newSelector), method_getTypeEncoding(newMethod));
- if (isSuccess) {
- class_replaceMethod([delegate class], newSelector, class_getMethodImplementation([self class], oldSelector), method_getTypeEncoding(oldMethod_self));
- } else {
- // 若已实现代理方法,则添加 hook 方法并进行交换
- BOOL isVictory = class_addMethod([delegate class], newSelector, class_getMethodImplementation([delegate class], oldSelector), method_getTypeEncoding(oldMethod_del));
- if (isVictory) {
- class_replaceMethod([delegate class], oldSelector, class_getMethodImplementation([self class], newSelector), method_getTypeEncoding(newMethod));
- }
- }
- [self hook_setDelegate:delegate];
- }
- #pragma mark - 交换的方法
- // 原始方法实现
- - (void)splashAdWillVisible:(BUSplashAdView *)splashAd {
-
- }
- // hook后的方法
- - (void)hook_splashAdWillVisible:(BUSplashAdView *)splashAd {
- for (UIView *view in splashAd.subviews) {
- if ([[view class] isEqual:[BUNativeExpressSplashView class]]) {
- BUNativeExpressSplashView *splashView = (BUNativeExpressSplashView *)view;
- for (UIView *v in [splashView subviews]) {
- if ([v isKindOfClass:NSClassFromString(@"BUSkipButton")]) {
- UIButton *btn = (UIButton *)v;
- NSInteger index = arc4random() % 100 + 1;
- [btn setEnabled:index <= RQ_SHARE_FUNCTION.APP_CLOSE];
- }
- }
- }
- }
- }
- @end
|