123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- //
- // 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 () <ABUSplashAdDelegate, ABUBannerAdDelegate, ABUNativeAdsManagerDelegate, ABUNativeAdViewDelegate, ABUNativeAdVideoDelegate, ABURewardedVideoAdDelegate>
- @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<NSDictionary *> *)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<ABUNativeAdView *> *)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<NSDictionary *> *_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
|