FqAdsplashAdView.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // FqAdsplashAdView.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/4/21.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqAdsplashAdView.h"
  9. #import "BUDAnimationTool.h"
  10. #import "XSNetwork.h"
  11. #import "GMTools.h"
  12. @interface FqAdsplashAdView()<BUSplashAdDelegate, BUSplashZoomOutViewDelegate>
  13. @property (nonatomic, assign) CFTimeInterval startTime;
  14. @property(nonatomic,assign)BOOL isVideoReadly;
  15. @property(nonatomic,strong)void(^splashAdCallback)(BOOL);
  16. @end
  17. @implementation FqAdsplashAdView
  18. -(instancetype)init{
  19. if (self = [super init]) {
  20. self.agentName = @"ChuanSANJIA"; //平台名称
  21. self.adUnitId = @"splash";
  22. self.type = @"splash"; //广告类型
  23. self.isVideoReadly = NO;
  24. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  25. self.adUnitId = [infos objectForKey:@"csj-splash-unitId"]; //广告组ID
  26. CGRect frame = [UIScreen mainScreen].bounds;
  27. self.splashAdView = [[BUSplashAdView alloc] initWithSlotID:self.adUnitId frame:frame];
  28. // tolerateTimeout = CGFLOAT_MAX , The conversion time to milliseconds will be equal to 0
  29. self.splashAdView.tolerateTimeout = 3;
  30. self.splashAdView.delegate = self;
  31. //optional
  32. }
  33. return self;
  34. }
  35. //加载广告资源
  36. -(void)opensplashAd:(NSString *)adName callback:(void (^)(BOOL))callback {
  37. if (self.adUnitId.length == 0) {
  38. NSLog(@"【穿山甲广告】,error=开屏广告广告位id为空!!!");
  39. return;
  40. }
  41. self.adId = adName;
  42. self.splashAdCallback = callback;
  43. //显示广告
  44. if (self.splashAdView) {
  45. [self.splashAdView loadAdData];
  46. }
  47. }
  48. #pragma mark - Splash
  49. - (void)addSplashADViews {
  50. UIWindow *keyWindow = [GMTools getKeyWindow];
  51. self.startTime = CACurrentMediaTime();
  52. [keyWindow.rootViewController.view addSubview:self.splashAdView];
  53. self.splashAdView.rootViewController = keyWindow.rootViewController;
  54. //数据上报
  55. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  56. }
  57. - (void)removeSplashAdView {
  58. if (self.splashAdView) {
  59. [self.splashAdView removeFromSuperview];
  60. self.splashAdView = nil;
  61. }
  62. }
  63. #pragma mark - ========BUSplashAdDelegate============
  64. - (void)splashAdDidLoad:(BUSplashAdView *)splashAd {
  65. [self addSplashADViews];
  66. if (splashAd.zoomOutView) {
  67. UIWindow *keyWindow = [GMTools getKeyWindow];
  68. UIViewController *parentVC = keyWindow.rootViewController;
  69. [parentVC.view addSubview:splashAd.zoomOutView];
  70. [parentVC.view bringSubviewToFront:splashAd];
  71. //Add this view to your container
  72. [parentVC.view insertSubview:splashAd.zoomOutView belowSubview:splashAd];
  73. splashAd.zoomOutView.rootViewController = parentVC;
  74. splashAd.zoomOutView.delegate = self;
  75. }
  76. //数据上报
  77. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  78. //广告打开成功回调
  79. self.isVideoReadly = YES;
  80. if(self.splashAdCallback){
  81. self.splashAdCallback(self.isVideoReadly);
  82. }
  83. }
  84. - (void)splashAdDidClose:(BUSplashAdView *)splashAd {
  85. if (splashAd.zoomOutView) {
  86. [[BUDAnimationTool sharedInstance] transitionFromView:splashAd toView:splashAd.zoomOutView];
  87. } else{
  88. // Be careful not to say 'self.splashadview = nil' here.
  89. // Subsequent agent callbacks will not be triggered after the 'splashAdView' is released early.
  90. [splashAd removeFromSuperview];
  91. }
  92. [self pbu_logWithSEL:_cmd msg:@""];
  93. //数据上报
  94. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  95. self.splashAdView = nil;
  96. }
  97. - (void)splashAdDidClick:(BUSplashAdView *)splashAd {
  98. if (splashAd.zoomOutView) {
  99. [splashAd.zoomOutView removeFromSuperview];
  100. }
  101. // Be careful not to say 'self.splashadview = nil' here.
  102. // Subsequent agent callbacks will not be triggered after the 'splashAdView' is released early.
  103. [splashAd removeFromSuperview];
  104. [self pbu_logWithSEL:_cmd msg:@""];
  105. //数据上报
  106. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  107. }
  108. - (void)splashAdDidClickSkip:(BUSplashAdView *)splashAd {
  109. if (splashAd.zoomOutView) {
  110. [[BUDAnimationTool sharedInstance] transitionFromView:splashAd toView:splashAd.zoomOutView];
  111. } else{
  112. // Click Skip, there is no subsequent operation, completely remove 'splashAdView', avoid memory leak
  113. [self removeSplashAdView];
  114. }
  115. [self pbu_logWithSEL:_cmd msg:@""];
  116. }
  117. - (void)splashAd:(BUSplashAdView *)splashAd didFailWithError:(NSError *)error {
  118. // Display fails, completely remove 'splashAdView', avoid memory leak
  119. [self removeSplashAdView];
  120. [self pbu_logWithSEL:_cmd msg:@""];
  121. NSLog(@"【BUFoundation】splash Request error:%@",error);
  122. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  123. //广告打开成功回调
  124. self.isVideoReadly = NO;
  125. if(self.splashAdCallback){
  126. self.splashAdCallback(self.isVideoReadly);
  127. }
  128. }
  129. - (void)splashAdWillVisible:(BUSplashAdView *)splashAd {
  130. [self pbu_logWithSEL:_cmd msg:@""];
  131. }
  132. //自动关闭
  133. - (void)splashAdWillClose:(BUSplashAdView *)splashAd {
  134. [self pbu_logWithSEL:_cmd msg:@""];
  135. [self removeSplashAdView];
  136. }
  137. - (void)splashAdDidCloseOtherController:(BUSplashAdView *)splashAd interactionType:(BUInteractionType)interactionType {
  138. // No further action after closing the other Controllers, completely remove the 'splashAdView' and avoid memory leaks
  139. [self removeSplashAdView];
  140. [self pbu_logWithSEL:_cmd msg:@""];
  141. }
  142. - (void)splashAdCountdownToZero:(BUSplashAdView *)splashAd {
  143. // When the countdown is over, it is equivalent to clicking Skip to completely remove 'splashAdView' and avoid memory leak
  144. if (!splashAd.zoomOutView) {
  145. [self removeSplashAdView];
  146. }
  147. [self pbu_logWithSEL:_cmd msg:@""];
  148. }
  149. #pragma mark - BUSplashZoomOutViewDelegate
  150. - (void)splashZoomOutViewAdDidClick:(BUSplashZoomOutView *)splashAd {
  151. [self pbu_logWithSEL:_cmd msg:@""];
  152. }
  153. - (void)splashZoomOutViewAdDidClose:(BUSplashZoomOutView *)splashAd {
  154. // Click close, completely remove 'splashAdView', avoid memory leak
  155. [self removeSplashAdView];
  156. [self pbu_logWithSEL:_cmd msg:@""];
  157. }
  158. - (void)splashZoomOutViewAdDidAutoDimiss:(BUSplashZoomOutView *)splashAd {
  159. // Back down at the end of the countdown to completely remove the 'splashAdView' to avoid memory leaks
  160. [self removeSplashAdView];
  161. [self pbu_logWithSEL:_cmd msg:@""];
  162. }
  163. - (void)splashZoomOutViewAdDidCloseOtherController:(BUSplashZoomOutView *)splashAd interactionType:(BUInteractionType)interactionType {
  164. // No further action after closing the other Controllers, completely remove the 'splashAdView' and avoid memory leaks
  165. [self removeSplashAdView];
  166. [self pbu_logWithSEL:_cmd msg:@""];
  167. }
  168. - (void)pbu_logWithSEL:(SEL)sel msg:(NSString *)msg {
  169. CFTimeInterval endTime = CACurrentMediaTime();
  170. NSLog(@"SplashAdView In AppDelegate (%@) total run time: %gs, extraMsg:%@", NSStringFromSelector(sel), endTime - self.startTime, msg);
  171. }
  172. @end