// // test.m // wallpaper_project // // Created by 忆海16 on 2024/8/2. // #import "initializationTopOn.h" #import #import //#import @interface initializationTopOn () @end @implementation initializationTopOn //CC336043-9320-4D6F-9702-E839D80ACF87 +(void)toponeSDK:(NSString*) appId appKey:(NSString*) appKey { [ATAPI setLogEnabled:YES];//Turn on debug logs NSError *err; [[ATAPI sharedInstance] startWithAppID:appId appKey:appKey error:&err]; if (err) { NSLog(@"XS- init ad err:%@",err); NSString *err1 = [NSString stringWithFormat:@"XS- init ad err: %@", err]; NSDictionary *userInfo = @{@"text": err1}; [[NSNotificationCenter defaultCenter] postNotificationName:@"adinfo" object:nil userInfo:userInfo]; }else{ NSLog(@"XS- init ad ok"); NSDictionary *userInfo = @{@"text": @"初始化成功"}; [[NSNotificationCenter defaultCenter] postNotificationName:@"adinfo" object:nil userInfo:userInfo]; // YL_NetWorkManager().requestRemoteIp() } } //+ (void)closeADWindow { // NSLog(@"已经执行closeADWindow....."); // UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; // [keyWindow.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // UIViewController *vc = obj.subviews.firstObject.nextResponder; // if ([vc isKindOfClass:[NSClassFromString(@"MTGVideoBaseViewController") class]]) { // // if ([vc respondsToSelector:@selector(closeButtonTappedAction:)]) { // NSLog(@"执行了skipVideo....."); // [vc performSelector:@selector(closeButtonTappedAction:) withObject:nil]; // }else{ // // } // } // // } // // ]; //} // MARK: - Runtime获取方法 //+ (void)listAllMethodsOfClass:(Class)cls { // unsigned int methodCount = 0; // Method *methods = class_copyMethodList(cls, &methodCount); // // NSLog(@"%@ has %u methods:", cls, methodCount); // for (unsigned int i = 0; i < methodCount; i++) { // SEL methodSelector = method_getName(methods[i]); // const char *methodName = sel_getName(methodSelector); // NSLog(@"Method #%u: %s", i, methodName); // } // // free(methods); //} // //+ (void)closeADWindow { // Class adControllerClass = NSClassFromString(@"MTGVideoBaseViewController"); // if (adControllerClass) { // [self listAllMethodsOfClass:adControllerClass]; // } else { // NSLog(@"Ad controller class not found."); // } //} + (void)removeADVCByDelayTime:(NSInteger)delayTime onclose: (OnClose) onclose { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime / 1000 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [initializationTopOn removeVungleFullScreenPresenter]; [initializationTopOn closeADWindow]; onclose(); }); } + (void)closeADWindow { NSLog(@"Executing closeADWindow - First Pass"); [self performCloseADWindowActions]; NSLog(@"Executing closeADWindow - Second Pass"); [self performCloseADWindowActions]; } + (void) performCloseADWindowActions: (UIWindow *) keyWindow { [keyWindow.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { UIViewController *vc = obj.subviews.firstObject.nextResponder; NSLog(@"XS- find Controller 2%@",vc.description); if ([vc isKindOfClass:[NSClassFromString(@"MTGVideoBaseViewController") class]]) { if ([vc respondsToSelector:@selector(_dismissViewControllerCompletion:)]) { NSLog(@"执行了 _dismissViewControllerCompletion:"); [vc performSelector:@selector(_dismissViewControllerCompletion:) withObject:nil]; *stop = YES; } else if ([vc respondsToSelector:@selector(callBackForDismiss)]) { NSLog(@"执行了 callBackForDismiss"); [vc performSelector:@selector(callBackForDismiss)]; *stop = YES; } else if ([vc respondsToSelector:@selector(didClose:)]) { NSLog(@"执行了 didClose:"); [vc performSelector:@selector(didClose:) withObject:nil]; *stop = YES; } } [self removeVungleFullScreenPresenter]; }]; } + (void)performCloseADWindowActions { // UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; // [initializationTopOn performCloseADWindowActions: keyWindow]; NSArray *windows = [UIApplication sharedApplication].windows; for (UIWindow *window in windows) { if(window.isKeyWindow) { [initializationTopOn performCloseADWindowActions: window]; break; } } } //+ (void)checkCloseWindown { // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [initializationTopOn removeVungleFullScreenPresenter]; // [initializationTopOn closeADWindow]; // // }); //} + (void)removeVungleFullScreenPresenter : (UIWindow *) keyWindow { UIViewController *foundViewController = [self findFullScreenPresenterInViewController:keyWindow.rootViewController]; if (foundViewController) { NSLog(@"Found FullScreenPresenter instance: %@", foundViewController); [initializationTopOn closeViewController:foundViewController]; // 关闭或移除 FullScreenPresenter // [self checkCloseWindown]; } else { NSLog(@"FullScreenPresenter not found."); } } + (void)removeVungleFullScreenPresenter { // UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; // [initializationTopOn removeVungleFullScreenPresenter: keyWindow]; NSArray *windows = [UIApplication sharedApplication].windows; for (UIWindow *window in windows) { if(window.isKeyWindow) { [initializationTopOn removeVungleFullScreenPresenter: window]; break; } } } + (UIViewController *)findFullScreenPresenterInViewController:(UIViewController *)viewController { NSLog(@"XS- find Controller 1 %@",viewController.description); if ([viewController isKindOfClass:NSClassFromString(@"VungleAdsSDK.FullScreenPresenter")]) { return viewController; } if ([viewController.description containsString:@"ISNProductViewController"]) { NSLog(@"XS- find Controller 1 %@ ok",viewController.description); return viewController; } // 递归遍历所有子控制器 for (UIViewController *childViewController in viewController.childViewControllers) { UIViewController *foundViewController = [self findFullScreenPresenterInViewController:childViewController]; if (foundViewController) { return foundViewController; } } // 检查 presentedViewController if (viewController.presentedViewController) { return [self findFullScreenPresenterInViewController:viewController.presentedViewController]; } return nil; } + (void)closeViewController:(UIViewController *)viewController { if (viewController.presentingViewController) { // 如果是模态展示的 [viewController dismissViewControllerAnimated:YES completion:^{ NSLog(@"XS- close ad view 1"); }]; } else if (viewController.navigationController) { // 如果在导航控制器中 [viewController.navigationController popViewControllerAnimated:YES]; NSLog(@"XS- close ad view 2"); } else { // 其他情况,尝试从父视图移除 [viewController.view removeFromSuperview]; [viewController removeFromParentViewController]; NSLog(@"XS- close ad view 3"); } } void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector) { Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } } @end @implementation UIButton (Swizzling) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"Swizzling UIButton sendAction:to:forEvent:"); swizzleMethod([self class], @selector(sendAction:to:forEvent:), @selector(xxx_sendAction:to:forEvent:)); }); } - (void)xxx_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { NSLog(@"Button clicked: %@", NSStringFromSelector(action)); [self xxx_sendAction:action to:target forEvent:event]; // 调用原始实现 } @end