RQADModule.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // RQADModule.m
  3. // YYXC
  4. //
  5. // Created by 张嵘 on 2023/3/21.
  6. // Copyright © 2023 JCZ. All rights reserved.
  7. //
  8. #import "RQADModule.h"
  9. #import "ABUNativeAdView+CustomRender.h"
  10. #define normal_splash_ID @"102293410"
  11. #define normal_native_ID @"102292784"
  12. #define normal_banner_ID @"102292951"
  13. #define normal_reward_ID @"102291892"
  14. static RQADModule *manger = nil;
  15. static dispatch_once_t onceToken;
  16. @interface RQADModule () <ABUSplashAdDelegate, ABUBannerAdDelegate, ABUNativeAdsManagerDelegate, ABUNativeAdViewDelegate, ABUNativeAdVideoDelegate, ABURewardedVideoAdDelegate>
  17. @property (nonatomic, readwrite, strong) GromoreDemoLoadAdView *adView;
  18. @property (nonatomic, readwrite, copy) RQADStatusChangeBlock aDStatusChangeBlock;
  19. @property (nonatomic, strong, nullable) ABUSplashAd *splashAd;
  20. @property (nonatomic, strong, nullable) ABUNativeAdsManager *nativeAd;
  21. @property (nonatomic, strong, nullable) ABUNativeAdView *nativeAdView;
  22. @property (nonatomic, strong, nullable) ABUBannerAd *bannerAd;
  23. @property (nonatomic, strong, nullable) ABURewardedVideoAd *rewardedVideoAd;
  24. @property (nonatomic, strong) UIView *bannerView;
  25. @property (nonatomic, strong) UIView *nativeView;
  26. @end
  27. @implementation RQADModule
  28. #pragma mark - SystemMethod
  29. + (RQADModule *)sharedManager {
  30. dispatch_once(&onceToken, ^{
  31. manger = [[self alloc] init];
  32. });
  33. return manger;
  34. }
  35. - (instancetype)init {
  36. self = [super init];
  37. if (self) {
  38. }
  39. return self;
  40. }
  41. - (void)dealloc {
  42. }
  43. #pragma mark - PublicMethod
  44. - (void)loadAdWithAdType:(GromoreAdType)adType customView:(UIView * _Nullable )customView {
  45. switch (adType) {
  46. case GromoreAdTypeSplash: {
  47. GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init];
  48. config.slotID = normal_splash_ID;
  49. config.adType = adType;
  50. [self loadSplashAdWithConfig:config andParam:[GromoreAdLoadParam new]];
  51. break;
  52. }
  53. case GromoreAdTypeBanner: {
  54. GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init];
  55. config.slotID = normal_banner_ID;
  56. config.adType = adType;
  57. config.adSize = customView.size;
  58. self.bannerView = customView;
  59. [self loadBannerAdWithConfig:config andParam:[GromoreAdLoadParam new]];
  60. break;
  61. }
  62. case GromoreAdTypeNative: {
  63. GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init];
  64. config.slotID = normal_native_ID;
  65. config.adType = adType;
  66. config.adSize = customView.size;
  67. self.nativeView = customView;
  68. [self loadNativeAdWithConfig:config andParam:[GromoreAdLoadParam new]];
  69. break;
  70. }
  71. case GromoreAdTypeRewardedVideo: {
  72. GromoreAdLoadConfig *config = [[GromoreAdLoadConfig alloc] init];
  73. config.slotID = normal_reward_ID;
  74. config.adType = adType;
  75. [self loadRewardedVideoAdWithConfig:config andParam:[GromoreAdLoadParam new]];
  76. break;
  77. }
  78. default:
  79. break;
  80. }
  81. }
  82. - (void)initADStatusChangeBlock:(RQADStatusChangeBlock)block {
  83. self.aDStatusChangeBlock = block;
  84. }
  85. - (void)closeAdWithAdType:(GromoreAdType)adType {
  86. switch (adType) {
  87. case GromoreAdTypeSplash: {
  88. break;
  89. }
  90. case GromoreAdTypeBanner: {
  91. if (self.bannerAd) {
  92. [self.bannerAd destory];
  93. self.bannerAd.delegate = nil;
  94. self.bannerAd = nil;
  95. }
  96. if (self.bannerView) {
  97. [self.bannerView removeAllSubviews];
  98. self.bannerView = nil;
  99. }
  100. break;
  101. }
  102. case GromoreAdTypeNative: {
  103. break;
  104. }
  105. case GromoreAdTypeRewardedVideo: {
  106. break;
  107. }
  108. default:
  109. break;
  110. }
  111. }
  112. #pragma mark - PrivateMethod
  113. - (void)loadSplashAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param {
  114. self.splashAd = [[ABUSplashAd alloc] initWithAdUnitID:config.slotID];
  115. self.splashAd.rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  116. self.splashAd.delegate = self;
  117. [self.splashAd loadAdData];
  118. }
  119. - (void)showSplashAd {
  120. [self.splashAd showInWindow:[UIApplication sharedApplication].keyWindow];
  121. }
  122. - (void)loadBannerAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param {
  123. self.bannerAd = [[ABUBannerAd alloc] initWithAdUnitID:config.slotID rootViewController:RQControllerHelper.currentViewController adSize:config.adSize];
  124. self.bannerAd.delegate = self;
  125. [self.bannerAd loadAdData];
  126. }
  127. - (void)loadNativeAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param {
  128. self.nativeAd = [[ABUNativeAdsManager alloc] initWithAdUnitID:config.slotID adSize:config.adSize];
  129. self.nativeAd.rootViewController = RQControllerHelper.currentViewController;
  130. self.nativeAd.startMutedIfCan = YES;
  131. self.nativeAd.delegate = self;
  132. [self.nativeAd addParam:[NSValue valueWithCGRect:CGRectMake(0, 0, 200, 100)] withKey:ABUAdLoadingParamNAExpectShakeViewFrame];
  133. #pragma mark 选用逻辑,可确保加载广告时有配置可用
  134. // 该逻辑用于判断配置是否拉取成功。如果拉取成功,可直接加载广告,否则需要调用setConfigSuccessCallback,传入block并在block中调用加载广告。SDK内部会在配置拉取成功后调用传入的block
  135. // 当前配置拉取成功,直接loadAdData
  136. if ([ABUAdSDKManager configDidLoad]) {
  137. [self.nativeAd loadAdDataWithCount:1];
  138. } else {
  139. // 当前配置未拉取成功,在成功之后会调用该callback
  140. [ABUAdSDKManager addConfigLoadSuccessObserver:self withAction:^(RQADModule * _Nonnull observer) {
  141. [observer.nativeAd loadAdDataWithCount:1];
  142. }];
  143. }
  144. }
  145. - (void)showNativeAd {
  146. [self showAdView:^UIView *(CGSize size) {
  147. self.nativeAdView.frame = (CGRect){CGPointZero, size};
  148. if (self.nativeAdView.isExpressAd) {
  149. [self.nativeAdView render];
  150. } else {
  151. [self.nativeAdView customRender];
  152. }
  153. return self.nativeAdView;
  154. }];
  155. }
  156. - (void)loadRewardedVideoAdWithConfig:(GromoreAdLoadConfig *)config andParam:(GromoreAdLoadParam *)param {
  157. self.rewardedVideoAd = [[ABURewardedVideoAd alloc] initWithAdUnitID:config.slotID];
  158. self.rewardedVideoAd.delegate = self;
  159. [self.rewardedVideoAd loadAdData];
  160. }
  161. - (void)showRewardedVideoAd {
  162. [self.rewardedVideoAd showAdFromRootViewController:RQControllerHelper.currentViewController];
  163. }
  164. #pragma mark - CommonMethod
  165. - (void)showAdView:(UIView *(^)(CGSize size))viewBlock {
  166. GromoreDemoAdDisplayView *view = (GromoreDemoAdDisplayView *)self.adBackView;
  167. [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  168. if (!viewBlock) return;
  169. UIView *v = viewBlock(view.frame.size);
  170. v ? [view addSubview:v] : (void*)0;
  171. }
  172. - (void)adStatusChanged:(GromoreDemoAdStatus)status {
  173. [_adView adStatusChanged:status];
  174. }
  175. #pragma mark ----- ABUSplashAdDelegate -----
  176. /// 加载成功回调
  177. - (void)splashAdDidLoad:(ABUSplashAd *)splashAd {
  178. [self showSplashAd];
  179. }
  180. /// 加载失败回调
  181. - (void)splashAd:(ABUSplashAd *)splashAd didFailWithError:(NSError *)error {
  182. }
  183. /// 展示成功回调
  184. - (void)splashAdWillVisible:(ABUSplashAd *)splashAd {
  185. }
  186. - (void)splashAdDidShowFailed:(ABUSplashAd *)splashAd error:(NSError *)error {
  187. }
  188. /// 广告点击回调
  189. - (void)splashAdDidClick:(ABUSplashAd *)splashAd {
  190. }
  191. /// 广告关闭回调
  192. - (void)splashAdDidClose:(ABUSplashAd *)splashAd {
  193. [splashAd destoryAd];
  194. }
  195. #pragma mark ----- ABUBannerAdDelegate -----
  196. /// 加载成功回调
  197. - (void)bannerAdDidLoad:(ABUBannerAd *)bannerAd bannerView:(UIView *)bannerView {
  198. [self.bannerView addSubview:bannerView];
  199. }
  200. /// 加载失败回调
  201. - (void)bannerAd:(ABUBannerAd *)bannerAd didLoadFailWithError:(NSError *)error {
  202. }
  203. /// 展示成功回调
  204. - (void)bannerAdDidBecomeVisible:(ABUBannerAd *)bannerAd bannerView:(UIView *)bannerView {
  205. self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeBanner, RQADDoType_Success) : nil;
  206. }
  207. /// 广告点击回调
  208. - (void)bannerAdDidClick:(ABUBannerAd *)ABUBannerAd bannerView:(UIView *)bannerView {
  209. }
  210. /// 广告关闭回调
  211. - (void)bannerAdDidClosed:(ABUBannerAd *)ABUBannerAd bannerView:(UIView *)bannerView dislikeWithReason:(NSArray<NSDictionary *> *)filterwords {
  212. // 可于此处移除广告view
  213. if (_bannerView) {
  214. [_bannerAd destory];
  215. [_bannerView removeFromSuperview];
  216. _bannerView = nil;
  217. }
  218. self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeBanner, RQADDoType_Close) : nil;
  219. }
  220. #pragma mark ----- ABUNativeAdsManagerDelegate -----
  221. /// 加载成功回调
  222. - (void)nativeAdsManagerSuccessToLoad:(ABUNativeAdsManager *)adsManager nativeAds:(NSArray<ABUNativeAdView *> *)nativeAdViewArray {
  223. // view取值,⚠️ 请慎重
  224. self.nativeAdView = nativeAdViewArray.firstObject;
  225. [self.nativeView addSubview:self.nativeAdView];
  226. // 重点:务必设定后续回调代理
  227. self.nativeAdView.delegate = self;
  228. self.nativeAdView.videoDelegate = self;
  229. self.nativeAdView.frame = (CGRect){CGPointZero, self.nativeView.size};
  230. if (self.nativeAdView.isExpressAd) {
  231. [self.nativeAdView render];
  232. } else {
  233. [self.nativeAdView customRender];
  234. }
  235. }
  236. /// 加载失败回调
  237. - (void)nativeAdsManager:(ABUNativeAdsManager *)adsManager didFailWithError:(NSError *)error {
  238. }
  239. #pragma mark - ABUNativeAdViewDelegate
  240. - (void)nativeAdExpressViewRenderSuccess:(ABUNativeAdView *_Nonnull)nativeExpressAdView {
  241. }
  242. - (void)nativeAdExpressViewRenderFail:(ABUNativeAdView *_Nonnull)nativeExpressAdView error:(NSError *_Nullable)error {
  243. }
  244. /// 展示成功回调
  245. - (void)nativeAdDidBecomeVisible:(ABUNativeAdView *_Nonnull)nativeAdView {
  246. }
  247. - (void)nativeAdExpressView:(ABUNativeAdView *_Nonnull)nativeAdView stateDidChanged:(ABUPlayerPlayState)playerState {
  248. }
  249. /// 广告点击回调
  250. - (void)nativeAdDidClick:(ABUNativeAdView *_Nonnull)nativeAdView withView:(UIView *_Nullable)view {
  251. }
  252. - (void)nativeAdViewWillPresentFullScreenModal:(ABUNativeAdView *_Nonnull)nativeAdView {
  253. }
  254. /// 广告关闭回调
  255. - (void)nativeAdExpressViewDidClosed:(ABUNativeAdView *_Nullable)nativeAdView closeReason:(NSArray<NSDictionary *> *_Nullable)filterWords {
  256. // 可于此处移除广告view
  257. }
  258. - (void)nativeAdShakeViewDidDismiss:(ABUNativeAdView *)nativeAdView {
  259. // shake view 消失
  260. NSLog(@"3.9.0.0 baidu 接收 shake view dismiss 回调");
  261. }
  262. - (void)nativeAdViewDidDismissFullScreenModal:(ABUNativeAdView *)nativeAdView {
  263. // 广告落地页关闭回调
  264. }
  265. - (void)nativeAdVideo:(ABUNativeAdView *)nativeAdView stateDidChanged:(ABUPlayerPlayState)playerState {
  266. }
  267. - (void)nativeAdVideoDidClick:(ABUNativeAdView *)nativeAdView {
  268. }
  269. - (void)nativeAdVideoDidPlayFinish:(ABUNativeAdView *)nativeAdView {
  270. }
  271. #pragma mark ----- ABURewardedVideoAdDelegate -----
  272. /// 加载成功回调
  273. - (void)rewardedVideoAdDidLoad:(ABURewardedVideoAd *)rewardedVideoAd {
  274. [self showRewardedVideoAd];
  275. }
  276. - (void)rewardedVideoAdDidDownLoadVideo:(ABURewardedVideoAd *)rewardedVideoAd {
  277. }
  278. /// 加载失败回调
  279. - (void)rewardedVideoAd:(ABURewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error {
  280. self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeRewardedVideo, RQADDoType_Faild) : nil;
  281. }
  282. /// 展示成功回调
  283. - (void)rewardedVideoAdDidVisible:(ABURewardedVideoAd *)rewardedVideoAd {
  284. self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeRewardedVideo, RQADDoType_Success) : nil;
  285. }
  286. - (void)rewardedVideoAdDidShowFailed:(ABURewardedVideoAd *)rewardedVideoAd error:(NSError *)error {
  287. self.aDStatusChangeBlock? self.aDStatusChangeBlock(GromoreAdTypeRewardedVideo, RQADDoType_Faild) : nil;
  288. }
  289. /// 广告点击回调
  290. - (void)rewardedVideoAdDidClick:(ABURewardedVideoAd *)rewardedVideoAd {
  291. }
  292. /// 广告关闭回调
  293. - (void)rewardedVideoAdDidClose:(ABURewardedVideoAd *)rewardedVideoAd {
  294. }
  295. - (void)rewardedVideoAd:(ABURewardedVideoAd *)rewardedVideoAd didPlayFinishWithError:(NSError *)error {
  296. }
  297. - (void)rewardedVideoAdDidSkip:(ABURewardedVideoAd *)rewardedVideoAd {
  298. }
  299. - (void)rewardedVideoAdServerRewardDidSucceed:(ABURewardedVideoAd *)rewardedVideoAd rewardInfo:(ABUAdapterRewardAdInfo *)rewardInfo verify:(BOOL)verify {
  300. if (verify) {
  301. RQ_COMMON_MANAGER.freeLookSimExamNum = 1;
  302. }
  303. }
  304. #pragma mark - LazyLoad
  305. - (UIView *)adBackView {
  306. return _adView.backView;
  307. }
  308. @end