FqGDTBannerAd.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // FqGDTBannerAd.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/4/30.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqGDTBannerAd.h"
  9. #import "GDTUnifiedBannerView.h"
  10. #import "GMTools.h"
  11. #import "XSNetwork.h"
  12. @interface FqGDTBannerAd()<GDTUnifiedBannerViewDelegate>
  13. @property (nonatomic, strong) GDTUnifiedBannerView *bannerView;
  14. @property(nonatomic,assign)BOOL isAdReadly; //广告是否准备好
  15. @property (nonatomic, assign)BOOL isAnimation;
  16. @property(nonatomic,strong)void(^bannerAdCallback)(BOOL);
  17. @end
  18. @implementation FqGDTBannerAd
  19. -(instancetype)init{
  20. if (self = [super init]) {
  21. self.agentName = @"YLH"; //平台名称
  22. self.adUnitId = @"Banner";
  23. self.type = @"Banner"; //广告类型
  24. self.isAdReadly = NO;
  25. self.isAnimation = NO;
  26. //
  27. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  28. if ([infos objectForKey:@"ylh-banner-unitId"]) {
  29. self.adUnitId = [infos objectForKey:@"ylh-banner-unitId"]; //广告组ID
  30. }
  31. if (self.adUnitId.length == 0) {
  32. NSLog(@"【优量汇广告】,error=模板2横幅广告位id为空!!!");
  33. }
  34. }
  35. return self;
  36. }
  37. #pragma mark - property getter
  38. -(GDTUnifiedBannerView *)bannerView {
  39. if (!_bannerView) {
  40. CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
  41. //CGRect rect = {CGPointZero, CGSizeMake(w1, 100)};
  42. CGFloat y = [GMTools isIPhoneXSeries]? 44.f:0.f;
  43. CGPoint orign = CGPointMake(0, y);
  44. CGRect rect = {orign, CGSizeMake(w1, 100)};
  45. UIWindow *fK = [GMTools getKeyWindow];
  46. self.bannerView = [[GDTUnifiedBannerView alloc]
  47. initWithFrame:rect
  48. placementId:self.adUnitId
  49. viewController:fK.rootViewController];
  50. self.bannerView.accessibilityIdentifier = @"banner_ad";
  51. self.bannerView.animated = self.isAnimation;
  52. self.bannerView.autoSwitchInterval = 30;
  53. self.bannerView.delegate = self;
  54. //数据上报
  55. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  56. }
  57. return _bannerView;
  58. }
  59. //打开广告
  60. -(void)openExpressBannerAd:(NSString *)adName callback:(void (^)(BOOL))callback {
  61. self.adId = adName;
  62. self.bannerAdCallback = callback;
  63. if (self.bannerView) {
  64. [self loadAdAndShowAd];
  65. }
  66. }
  67. //加载显示广告
  68. -(void)loadAdAndShowAd {
  69. if (self.bannerView.superview) {
  70. [self.bannerView removeFromSuperview];
  71. self.bannerView = nil;
  72. }
  73. //获取屏幕最上层VC
  74. UIViewController *vc = [GMTools getViewControl];
  75. [vc.view addSubview:self.bannerView];
  76. [self.bannerView loadAdAndShow];
  77. //数据上报
  78. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  79. }
  80. #pragma mark - GDTUnifiedBannerViewDelegate
  81. /**
  82. * 请求广告条数据成功后调用
  83. * 当接收服务器返回的广告数据成功后调用该函数
  84. */
  85. - (void)unifiedBannerViewDidLoad:(GDTUnifiedBannerView *)unifiedBannerView
  86. {
  87. NSLog(@"%s",__FUNCTION__);
  88. NSLog(@"unified banner did load");
  89. self.isAdReadly = YES;
  90. if(self.bannerAdCallback){
  91. self.bannerAdCallback(self.isAdReadly);
  92. }
  93. }
  94. /**
  95. * 请求广告条数据失败后调用
  96. * 当接收服务器返回的广告数据失败后调用该函数
  97. */
  98. - (void)unifiedBannerViewFailedToLoad:(GDTUnifiedBannerView *)unifiedBannerView error:(NSError *)error
  99. {
  100. NSLog(@"%s",__FUNCTION__);
  101. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  102. self.isAdReadly = NO;
  103. if(self.bannerAdCallback){
  104. self.bannerAdCallback(self.isAdReadly);
  105. }
  106. }
  107. /**
  108. * banner2.0曝光回调
  109. */
  110. - (void)unifiedBannerViewWillExpose:(nonnull GDTUnifiedBannerView *)unifiedBannerView {
  111. NSLog(@"%s",__FUNCTION__);
  112. }
  113. /**
  114. * banner2.0点击回调
  115. */
  116. - (void)unifiedBannerViewClicked:(GDTUnifiedBannerView *)unifiedBannerView
  117. {
  118. NSLog(@"%s",__FUNCTION__);
  119. }
  120. /**
  121. * 应用进入后台时调用
  122. * 当点击应用下载或者广告调用系统程序打开,应用将被自动切换到后台
  123. */
  124. - (void)unifiedBannerViewWillLeaveApplication:(GDTUnifiedBannerView *)unifiedBannerView
  125. {
  126. NSLog(@"%s",__FUNCTION__);
  127. }
  128. /**
  129. * 全屏广告页已经被关闭
  130. */
  131. - (void)unifiedBannerViewDidDismissFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
  132. {
  133. NSLog(@"%s",__FUNCTION__);
  134. }
  135. /**
  136. * 全屏广告页即将被关闭
  137. */
  138. - (void)unifiedBannerViewWillDismissFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
  139. {
  140. NSLog(@"%s",__FUNCTION__);
  141. }
  142. /**
  143. * banner2.0广告点击以后即将弹出全屏广告页
  144. */
  145. - (void)unifiedBannerViewWillPresentFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
  146. {
  147. NSLog(@"%s",__FUNCTION__);
  148. }
  149. /**
  150. * banner2.0广告点击以后弹出全屏广告页完毕
  151. */
  152. - (void)unifiedBannerViewDidPresentFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
  153. {
  154. NSLog(@"%s",__FUNCTION__);
  155. }
  156. /**
  157. * banner2.0被用户关闭时调用
  158. */
  159. - (void)unifiedBannerViewWillClose:(nonnull GDTUnifiedBannerView *)unifiedBannerView {
  160. //数据上报
  161. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  162. [self.bannerView removeFromSuperview];
  163. self.bannerView = nil;
  164. NSLog(@"%s",__FUNCTION__);
  165. }
  166. @end