// // RQADModule.m // YYXC // // Created by 张嵘 on 2023/3/21. // Copyright © 2023 JCZ. All rights reserved. // #import "RQADModule.h" #import "ABUNativeAdView+CustomRender.h" #define normal_splash_ID @"102293410" #define normal_native_ID @"102292784" #define normal_banner_ID @"102292951" #define normal_reward_ID @"102291892" static RQADModule *manger = nil; static dispatch_once_t onceToken; @interface RQADModule () @property (nonatomic, readwrite, strong) GromoreDemoLoadAdView *adView; @property (nonatomic, readwrite, copy) RQADStatusChangeBlock aDStatusChangeBlock; @property (nonatomic, strong, nullable) ABUSplashAd *splashAd; @property (nonatomic, strong, nullable) ABUNativeAdsManager *nativeAd; @property (nonatomic, strong, nullable) ABUNativeAdView *nativeAdView; @property (nonatomic, strong, nullable) ABUBannerAd *bannerAd; @property (nonatomic, strong, nullable) ABURewardedVideoAd *rewardedVideoAd; @property (nonatomic, strong) UIView *bannerView; @property (nonatomic, strong) UIView *nativeView; @end @implementation RQADModule #pragma mark - SystemMethod + (RQADModule *)sharedManager { dispatch_once(&onceToken, ^{ manger = [[self alloc] init]; }); return manger; } - (instancetype)init { self = [super init]; if (self) { } return self; } - (void)dealloc { } #pragma mark - PublicMethod - (void)loadAdWithAdType:(GromoreAdType)adType customView:(UIView * _Nullable )customView { switch (adType) { case GromoreAdTypeSplash: { GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init]; config.slotID = normal_splash_ID; config.adType = adType; [self loadSplashAdWithConfig:config andParam:[GromoreAdLoadParam new]]; break; } case GromoreAdTypeBanner: { GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init]; config.slotID = normal_banner_ID; config.adType = adType; config.adSize = customView.size; self.bannerView = customView; [self loadBannerAdWithConfig:config andParam:[GromoreAdLoadParam new]]; break; } case GromoreAdTypeNative: { GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init]; config.slotID = normal_native_ID; config.adType = adType; config.adSize = customView.size; self.nativeView = customView; [self loadNativeAdWithConfig:config andParam:[GromoreAdLoadParam new]]; break; } case GromoreAdTypeRewardedVideo: { GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init]; config.slotID = normal_reward_ID; config.adType = adType; [self loadRewardedVideoAdWithConfig:config andParam:[GromoreAdLoadParam new]]; break; } default: break; } } - (void)initADStatusChangeBlock:(RQADStatusChangeBlock)block { self.aDStatusChangeBlock = block; } - (void)closeAdWithAdType:(GromoreAdType)adType { switch (adType) { case GromoreAdTypeSplash: { break; } case GromoreAdTypeBanner: { if (self.bannerAd) { [self.bannerAd destory]; self.bannerAd.delegate = nil; self.bannerAd = nil; } if (self.bannerView) { [self.bannerView removeAllSubviews]; self.bannerView = nil; } break; } case GromoreAdTypeNative: { break; } case GromoreAdTypeRewardedVideo: { break; } default: break; } } #pragma mark - PrivateMethod - (void)loadSplashAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param { self.splashAd = [[ABUSplashAd alloc] initWithAdUnitID:config.slotID]; self.splashAd.rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; self.splashAd.delegate = self; [self.splashAd loadAdData]; } - (void)showSplashAd { [self.splashAd showInWindow:[UIApplication sharedApplication].keyWindow]; } - (void)loadBannerAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param { self.bannerAd = [[ABUBannerAd alloc] initWithAdUnitID:config.slotID rootViewController:RQControllerHelper.currentViewController adSize:config.adSize]; self.bannerAd.delegate = self; [self.bannerAd loadAdData]; } - (void)loadNativeAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param { self.nativeAd = [[ABUNativeAdsManager alloc] initWithAdUnitID:config.slotID adSize:config.adSize]; self.nativeAd.rootViewController = RQControllerHelper.currentViewController; self.nativeAd.startMutedIfCan = YES; self.nativeAd.delegate = self; [self.nativeAd addParam:[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 100)] withKey:ABUAdLoadingParamNAExpectShakeViewFrame]; #pragma mark 选用逻辑,可确保加载广告时有配置可用 // 该逻辑用于判断配置是否拉取成功。如果拉取成功,可直接加载广告,否则需要调用setConfigSuccessCallback,传入block并在block中调用加载广告。SDK内部会在配置拉取成功后调用传入的block // 当前配置拉取成功,直接loadAdData if ([ABUAdSDKManager configDidLoad]) { [self.nativeAd loadAdDataWithCount:1]; } else { // 当前配置未拉取成功,在成功之后会调用该callback [ABUAdSDKManager addConfigLoadSuccessObserver:self withAction:^(RQADModule * _Nonnull observer) { [observer.nativeAd loadAdDataWithCount:1]; }]; } } - (void)showNativeAd { [self showAdView:^UIView *(CGSize size) { self.nativeAdView.frame = (CGRect){CGPointZero, size}; if (self.nativeAdView.isExpressAd) { [self.nativeAdView render]; } else { [self.nativeAdView customRender]; } return self.nativeAdView; }]; } - (void)loadRewardedVideoAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param { self.rewardedVideoAd = [[ABURewardedVideoAd alloc] initWithAdUnitID:config.slotID]; self.rewardedVideoAd.delegate = self; [self.rewardedVideoAd loadAdData]; } - (void)showRewardedVideoAd { [self.rewardedVideoAd showAdFromRootViewController:RQControllerHelper.currentViewController]; } #pragma mark - CommonMethod - (void)showAdView:(UIView *(^)(CGSize size))viewBlock { GromoreDemoAdDisplayView *view = (GromoreDemoAdDisplayView *)self.adBackView; [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; if (!viewBlock) return; UIView *v = viewBlock(view.frame.size); v ? [view addSubview:v] : (void*)0; } - (void)adStatusChanged:(GromoreDemoAdStatus)status { [_adView adStatusChanged:status]; } #pragma mark ----- ABUSplashAdDelegate ----- /// 加载成功回调 - (void)splashAdDidLoad:(ABUSplashAd *)splashAd { [self showSplashAd]; } /// 加载失败回调 - (void)splashAd:(ABUSplashAd *)splashAd didFailWithError:(NSError *)error { } /// 展示成功回调 - (void)splashAdWillVisible:(ABUSplashAd *)splashAd { } - (void)splashAdDidShowFailed:(ABUSplashAd *)splashAd error:(NSError *)error { } /// 广告点击回调 - (void)splashAdDidClick:(ABUSplashAd *)splashAd { } /// 广告关闭回调 - (void)splashAdDidClose:(ABUSplashAd *)splashAd { [splashAd destoryAd]; } #pragma mark ----- ABUBannerAdDelegate ----- /// 加载成功回调 - (void)bannerAdDidLoad:(ABUBannerAd *)bannerAd bannerView:(UIView *)bannerView { [self.bannerView addSubview:bannerView]; } /// 加载失败回调 - (void)bannerAd:(ABUBannerAd *)bannerAd didLoadFailWithError:(NSError *)error { } /// 展示成功回调 - (void)bannerAdDidBecomeVisible:(ABUBannerAd *)bannerAd bannerView:(UIView *)bannerView { self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeBanner, RQADDoType_Success) : nil; } /// 广告点击回调 - (void)bannerAdDidClick:(ABUBannerAd *)ABUBannerAd bannerView:(UIView *)bannerView { } /// 广告关闭回调 - (void)bannerAdDidClosed:(ABUBannerAd *)ABUBannerAd bannerView:(UIView *)bannerView dislikeWithReason:(NSArray *)filterwords { // 可于此处移除广告view if (_bannerView) { [_bannerAd destory]; [_bannerView removeFromSuperview]; _bannerView = nil; } self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeBanner, RQADDoType_Close) : nil; } #pragma mark ----- ABUNativeAdsManagerDelegate ----- /// 加载成功回调 - (void)nativeAdsManagerSuccessToLoad:(ABUNativeAdsManager *)adsManager nativeAds:(NSArray *)nativeAdViewArray { // view取值,⚠️ 请慎重 self.nativeAdView = nativeAdViewArray.firstObject; [self.nativeView addSubview:self.nativeAdView]; // 重点:务必设定后续回调代理 self.nativeAdView.delegate = self; self.nativeAdView.videoDelegate = self; self.nativeAdView.frame = (CGRect){CGPointZero, self.nativeView.size}; if (self.nativeAdView.isExpressAd) { [self.nativeAdView render]; } else { [self.nativeAdView customRender]; } } /// 加载失败回调 - (void)nativeAdsManager:(ABUNativeAdsManager *)adsManager didFailWithError:(NSError *)error { } #pragma mark - ABUNativeAdViewDelegate - (void)nativeAdExpressViewRenderSuccess:(ABUNativeAdView *_Nonnull)nativeExpressAdView { } - (void)nativeAdExpressViewRenderFail:(ABUNativeAdView *_Nonnull)nativeExpressAdView error:(NSError *_Nullable)error { } /// 展示成功回调 - (void)nativeAdDidBecomeVisible:(ABUNativeAdView *_Nonnull)nativeAdView { } - (void)nativeAdExpressView:(ABUNativeAdView *_Nonnull)nativeAdView stateDidChanged:(ABUPlayerPlayState)playerState { } /// 广告点击回调 - (void)nativeAdDidClick:(ABUNativeAdView *_Nonnull)nativeAdView withView:(UIView *_Nullable)view { } - (void)nativeAdViewWillPresentFullScreenModal:(ABUNativeAdView *_Nonnull)nativeAdView { } /// 广告关闭回调 - (void)nativeAdExpressViewDidClosed:(ABUNativeAdView *_Nullable)nativeAdView closeReason:(NSArray *_Nullable)filterWords { // 可于此处移除广告view } - (void)nativeAdShakeViewDidDismiss:(ABUNativeAdView *)nativeAdView { // shake view 消失 NSLog(@"3.9.0.0 baidu 接收 shake view dismiss 回调"); } - (void)nativeAdViewDidDismissFullScreenModal:(ABUNativeAdView *)nativeAdView { // 广告落地页关闭回调 } - (void)nativeAdVideo:(ABUNativeAdView *)nativeAdView stateDidChanged:(ABUPlayerPlayState)playerState { } - (void)nativeAdVideoDidClick:(ABUNativeAdView *)nativeAdView { } - (void)nativeAdVideoDidPlayFinish:(ABUNativeAdView *)nativeAdView { } #pragma mark ----- ABURewardedVideoAdDelegate ----- /// 加载成功回调 - (void)rewardedVideoAdDidLoad:(ABURewardedVideoAd *)rewardedVideoAd { [self showRewardedVideoAd]; } - (void)rewardedVideoAdDidDownLoadVideo:(ABURewardedVideoAd *)rewardedVideoAd { } /// 加载失败回调 - (void)rewardedVideoAd:(ABURewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error { self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeRewardedVideo, RQADDoType_Faild) : nil; } /// 展示成功回调 - (void)rewardedVideoAdDidVisible:(ABURewardedVideoAd *)rewardedVideoAd { self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeRewardedVideo, RQADDoType_Success) : nil; } - (void)rewardedVideoAdDidShowFailed:(ABURewardedVideoAd *)rewardedVideoAd error:(NSError *)error { self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeRewardedVideo, RQADDoType_Faild) : nil; } /// 广告点击回调 - (void)rewardedVideoAdDidClick:(ABURewardedVideoAd *)rewardedVideoAd { } /// 广告关闭回调 - (void)rewardedVideoAdDidClose:(ABURewardedVideoAd *)rewardedVideoAd { } - (void)rewardedVideoAd:(ABURewardedVideoAd *)rewardedVideoAd didPlayFinishWithError:(NSError *)error { } - (void)rewardedVideoAdDidSkip:(ABURewardedVideoAd *)rewardedVideoAd { } - (void)rewardedVideoAdServerRewardDidSucceed:(ABURewardedVideoAd *)rewardedVideoAd rewardInfo:(ABUAdapterRewardAdInfo *)rewardInfo verify:(BOOL)verify { if (verify) { RQ_COMMON_MANAGER.freeLookSimExamNum = 1; } } #pragma mark - LazyLoad - (UIView *)adBackView { return _adView.backView; } @end