BUSplashAdView+RQExtension.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // BUSplashAdView+RQExtension.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2021/4/6.
  6. // Copyright © 2021 JCZ. All rights reserved.
  7. //
  8. #import "BUSplashAdView+RQExtension.h"
  9. #import <objc/runtime.h>
  10. @implementation BUSplashAdView (RQExtension)
  11. #pragma mark - load
  12. + (void)load {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. Method oldMethod = class_getInstanceMethod([self class], @selector(setDelegate:));
  16. Method newMethod = class_getInstanceMethod([self class], @selector(hook_setDelegate:));
  17. method_exchangeImplementations(oldMethod, newMethod);
  18. });
  19. }
  20. #pragma mark - 交换代理方法
  21. - (void)hook_setDelegate:(id<BUSplashAdDelegate>)delegate {
  22. SEL oldSelector = @selector(splashAdWillVisible:);
  23. SEL newSelector = @selector(hook_splashAdWillVisible:);
  24. Method oldMethod_del = class_getInstanceMethod([delegate class], oldSelector);
  25. Method oldMethod_self = class_getInstanceMethod([self class], oldSelector);
  26. Method newMethod = class_getInstanceMethod([self class], newSelector);
  27. // 若未实现代理方法,则先添加代理方法
  28. BOOL isSuccess = class_addMethod([delegate class], oldSelector, class_getMethodImplementation([self class], newSelector), method_getTypeEncoding(newMethod));
  29. if (isSuccess) {
  30. class_replaceMethod([delegate class], newSelector, class_getMethodImplementation([self class], oldSelector), method_getTypeEncoding(oldMethod_self));
  31. } else {
  32. // 若已实现代理方法,则添加 hook 方法并进行交换
  33. BOOL isVictory = class_addMethod([delegate class], newSelector, class_getMethodImplementation([delegate class], oldSelector), method_getTypeEncoding(oldMethod_del));
  34. if (isVictory) {
  35. class_replaceMethod([delegate class], oldSelector, class_getMethodImplementation([self class], newSelector), method_getTypeEncoding(newMethod));
  36. }
  37. }
  38. [self hook_setDelegate:delegate];
  39. }
  40. #pragma mark - 交换的方法
  41. // 原始方法实现
  42. - (void)splashAdWillVisible:(BUSplashAdView *)splashAd {
  43. }
  44. // hook后的方法
  45. - (void)hook_splashAdWillVisible:(BUSplashAdView *)splashAd {
  46. for (UIView *view in splashAd.subviews) {
  47. if ([[view class] isEqual:[BUNativeExpressSplashView class]]) {
  48. BUNativeExpressSplashView *splashView = (BUNativeExpressSplashView *)view;
  49. for (UIView *v in [splashView subviews]) {
  50. if ([v isKindOfClass:NSClassFromString(@"BUSkipButton")]) {
  51. UIButton *btn = (UIButton *)v;
  52. NSInteger index = arc4random() % 100 + 1;
  53. [btn setEnabled:index <= RQ_SHARE_FUNCTION.APP_CLOSE];
  54. }
  55. }
  56. }
  57. }
  58. }
  59. @end