FqGDTExpressNativeAd.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // FqGDTExpressNativeAd.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/5/8.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqGDTExpressNativeAd.h"
  9. #import "GDTNativeExpressAd.h"
  10. #import "GDTNativeExpressAdView.h"
  11. #import "GMTools.h"
  12. #import <UIKit/UIKit.h>
  13. #import "XSNetwork.h"
  14. @interface FqGDTExpressNativeAd()<GDTNativeExpressAdDelegete>
  15. @property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
  16. @property (nonatomic, strong) NSMutableArray *expressAdViews;
  17. @property(nonatomic,assign)CGRect rect;
  18. @property(nonatomic,assign)BOOL isAdReadly;
  19. @property(nonatomic,strong)void(^nativeAdCallback)(BOOL);
  20. @end
  21. @implementation FqGDTExpressNativeAd
  22. -(instancetype)init{
  23. if (self = [super init]) {
  24. self.agentName = @"YLH"; //平台名称
  25. self.adUnitId = @"Native";
  26. self.type = @"Native"; //广告类型
  27. self.isAdReadly = NO;
  28. // NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  29. // if ([infos objectForKey:@"ylh-native-unitId"]) {
  30. // self.adUnitId = [infos objectForKey:@"ylh-native-unitId"]; //广告组ID
  31. // }
  32. }
  33. return self;
  34. }
  35. //关闭广告
  36. -(void)closeExpressNatived:(NSString *)adName {
  37. if ([adName isEqualToString:self.adUnitId]) {
  38. if (self.expressAdViews.count > 0) {
  39. GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject];
  40. [childView removeFromSuperview];
  41. [self.expressAdViews removeAllObjects];
  42. self.nativeExpressAd = nil;
  43. }
  44. }else{
  45. NSLog(@"关闭的:模板2原生广告位id不匹配!");
  46. }
  47. }
  48. //打开广告
  49. -(void)openExpressNativeAdWithPlacementId:(NSString *)placementId AdName:(NSString *)adName Frame:(CGRect)rect callback:(void (^)(BOOL))callback {
  50. self.adId = adName;
  51. self.nativeAdCallback = callback;
  52. self.adUnitId = placementId;
  53. self.rect = rect;
  54. if (self.adUnitId.length == 0) {
  55. NSLog(@"【优量汇广告】,error=模板2原生广告位id为空!!!");
  56. [XSNetwork showHudFailure:@"广告位id位空!"];
  57. return;
  58. }
  59. [self InitExpressNativedAdWithPlacementId:self.adUnitId adSize:CGSizeMake(rect.size.width, rect.size.height)];
  60. }
  61. //原生广告
  62. -(void)InitExpressNativedAdWithPlacementId:(NSString *)placementId adSize:(CGSize)size{
  63. self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithPlacementId:placementId
  64. adSize:size];
  65. self.nativeExpressAd.delegate = self;
  66. [self.nativeExpressAd loadAd:3];
  67. //数据上报
  68. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  69. }
  70. #pragma mark - GDTNativeExpressAdDelegete
  71. /**
  72. * 拉取广告成功的回调
  73. */
  74. - (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd *)nativeExpressAd views:(NSArray<__kindof GDTNativeExpressAdView *> *)views
  75. {
  76. NSLog(@"%s",__FUNCTION__);
  77. self.expressAdViews = [NSMutableArray arrayWithArray:views];
  78. UIWindow *keyWindow = [GMTools getKeyWindow];
  79. if (self.expressAdViews.count) {
  80. [self.expressAdViews enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  81. GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
  82. expressView.controller = keyWindow.rootViewController;
  83. [expressView render];
  84. NSLog(@"eCPM:%ld eCPMLevel:%@", [expressView eCPM], [expressView eCPMLevel]);
  85. }];
  86. }
  87. UIViewController *v1 = [GMTools getViewControl];
  88. GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject];
  89. childView.frame = self.rect;
  90. [v1.view addSubview:childView];
  91. }
  92. /**
  93. * 拉取广告失败的回调
  94. */
  95. - (void)nativeExpressAdRenderFail:(GDTNativeExpressAdView *)nativeExpressAdView
  96. {
  97. NSLog(@"%s",__FUNCTION__);
  98. }
  99. /**
  100. * 拉取原生模板广告失败
  101. */
  102. - (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error
  103. {
  104. NSLog(@"%s",__FUNCTION__);
  105. NSLog(@"Express Ad Load Fail : %@",error);
  106. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  107. }
  108. - (void)nativeExpressAdViewRenderSuccess:(GDTNativeExpressAdView *)nativeExpressAdView
  109. {
  110. NSLog(@"%s",__FUNCTION__);
  111. UIViewController *v1 = [GMTools getViewControl];
  112. GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject];
  113. childView.frame = self.rect;
  114. [v1.view addSubview:childView];
  115. //数据上报
  116. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  117. }
  118. - (void)nativeExpressAdViewClicked:(GDTNativeExpressAdView *)nativeExpressAdView
  119. {
  120. NSLog(@"%s",__FUNCTION__);
  121. //数据上报
  122. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  123. }
  124. - (void)nativeExpressAdViewClosed:(GDTNativeExpressAdView *)nativeExpressAdView
  125. {
  126. NSLog(@"%s",__FUNCTION__);
  127. //数据上报
  128. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  129. //关闭广告.
  130. GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject];
  131. [childView removeFromSuperview];
  132. [self.expressAdViews removeObject:nativeExpressAdView];
  133. self.nativeExpressAd = nil;
  134. NSLog(@"[优量汇]原生广告关闭成功!");
  135. }
  136. - (void)nativeExpressAdViewExposure:(GDTNativeExpressAdView *)nativeExpressAdView
  137. {
  138. NSLog(@"%s",__FUNCTION__);
  139. }
  140. - (void)nativeExpressAdViewWillPresentScreen:(GDTNativeExpressAdView *)nativeExpressAdView
  141. {
  142. NSLog(@"%s",__FUNCTION__);
  143. }
  144. - (void)nativeExpressAdViewDidPresentScreen:(GDTNativeExpressAdView *)nativeExpressAdView
  145. {
  146. NSLog(@"%s",__FUNCTION__);
  147. }
  148. - (void)nativeExpressAdViewWillDismissScreen:(GDTNativeExpressAdView *)nativeExpressAdView
  149. {
  150. NSLog(@"%s",__FUNCTION__);
  151. }
  152. - (void)nativeExpressAdViewDidDismissScreen:(GDTNativeExpressAdView *)nativeExpressAdView
  153. {
  154. NSLog(@"%s",__FUNCTION__);
  155. }
  156. /**
  157. * 详解:当点击应用下载或者广告调用系统程序打开时调用
  158. */
  159. - (void)nativeExpressAdViewApplicationWillEnterBackground:(GDTNativeExpressAdView *)nativeExpressAdView
  160. {
  161. NSLog(@"--------%s-------",__FUNCTION__);
  162. }
  163. @end