FqGTDFullScreenVideoAd.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // FqGTDFullScreenVideoAd.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/4/29.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqGTDFullScreenVideoAd.h"
  9. #import "GDTExpressInterstitialAd.h"
  10. #import "XSNetwork.h"
  11. #import "GMTools.h"
  12. @interface FqGTDFullScreenVideoAd()<GDTExpressInterstitialAdDelegate>
  13. @property (nonatomic, strong) GDTExpressInterstitialAd *interstitial;
  14. @property(nonatomic,assign)BOOL isAdReadly; //广告是否准备好
  15. @property(nonatomic,strong)void(^fullScreenAdCallback)(BOOL);
  16. @end
  17. @implementation FqGTDFullScreenVideoAd
  18. -(instancetype)init{
  19. if (self = [super init]) {
  20. self.agentName = @"YLH"; //平台名称
  21. self.adUnitId = @"fullScreen";
  22. self.type = @"fullScreen"; //广告类型
  23. self.isAdReadly = NO;
  24. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  25. if ([infos objectForKey:@"ylh-fullScreen-unitId"]) {
  26. self.adUnitId = [infos objectForKey:@"ylh-fullScreen-unitId"]; //广告组ID
  27. }
  28. if (self.adUnitId.length == 0) {
  29. NSLog(@"【优量汇广告】,error=模板2插屏全屏广告位id为空!!!");
  30. }
  31. //预加载广告
  32. [self loadFullScreenAdWithPlacementId:self.adUnitId];
  33. }
  34. return self;
  35. }
  36. //打开广告
  37. -(void)openFullScreenVideoAd:(NSString *)adName callback:(void (^)(BOOL))callback {
  38. self.adId = adName;
  39. self.fullScreenAdCallback = callback;
  40. if (self.interstitial) {
  41. [self.interstitial loadFullScreenAd];
  42. }
  43. }
  44. //拉取广告
  45. - (void)loadFullScreenAdWithPlacementId:(NSString *)placementId {
  46. if (placementId.length == 0) {
  47. NSLog(@"[优量汇SDK],模板2插屏全屏广告id为空!!!");
  48. return;
  49. }
  50. if (self.interstitial) {
  51. self.interstitial.delegate = nil;
  52. }
  53. self.interstitial = [[GDTExpressInterstitialAd alloc] initWithPlacementId:placementId];
  54. self.interstitial.delegate = self;
  55. self.interstitial.videoMuted = YES; // 设置视频是否Mute,默认自动播放下静音
  56. //self.interstitial.maxVideoDuration = 30; // 如果需要设置视频最大时长,可以通过这个参数来进行设置
  57. //[self.interstitial loadFullScreenAd];
  58. //数据上报
  59. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  60. }
  61. //显示广告
  62. - (void)showInterstitialAd {
  63. UIWindow *fK = [GMTools getKeyWindow];
  64. [self.interstitial presentFullScreenAdFromRootViewController:fK.rootViewController];
  65. //数据上报
  66. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  67. }
  68. #pragma mark - =========GDTExpressInterstitialAdDelegate===========
  69. /**
  70. * 模板插屏半屏广告加载成功回调
  71. * 当接收服务器返回的广告数据成功且模板渲染成功后调用该函数
  72. */
  73. - (void)expressInterstitialSuccessToLoadAd:(GDTExpressInterstitialAd *)unifiedInterstitial
  74. {
  75. NSLog(@"%s",__FUNCTION__);
  76. NSLog(@"%@",[NSString stringWithFormat:@"插屏状态:%@",@"Load Success." ]);
  77. NSLog(@"videoDuration:%lf isVideo: %@", unifiedInterstitial.videoDuration, @(unifiedInterstitial.isVideoAd));
  78. [self showInterstitialAd];
  79. }
  80. /**
  81. * 模板插屏半屏广告预加载失败回调
  82. * 当接收服务器返回的广告数据失败或模板渲染失败后调用该函数
  83. */
  84. - (void)expressInterstitialFailToLoadAd:(GDTExpressInterstitialAd *)unifiedInterstitial error:(NSError *)error
  85. {
  86. NSLog(@"%s",__FUNCTION__);
  87. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  88. NSLog(@"interstitial fail to load, Error : %@",error);
  89. self.isAdReadly = NO;
  90. if(self.fullScreenAdCallback){
  91. self.fullScreenAdCallback(self.isAdReadly);
  92. }
  93. }
  94. /**
  95. * 模板插屏半屏广告将要展示回调
  96. * 模板插屏半屏广告即将展示回调该函数
  97. */
  98. - (void)expressInterstitialWillPresentScreen:(GDTExpressInterstitialAd *)unifiedInterstitial
  99. {
  100. NSLog(@"%s",__FUNCTION__);
  101. NSLog(@"%@",[NSString stringWithFormat:@"插屏状态:%@",@"Going to present." ]);
  102. }
  103. /**
  104. * 模板插屏半屏广告展示失败回调
  105. * 模板插屏半屏展示失败时回调该函数
  106. */
  107. - (void)expressInterstitialFailToPresent:(GDTExpressInterstitialAd *)unifiedInterstitial error:(NSError *)error {
  108. NSLog(@"%s",__FUNCTION__);
  109. NSLog(@"%@",[NSString stringWithFormat:@"插屏状态:%@",@"fail to present." ]);
  110. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  111. self.isAdReadly = NO;
  112. if(self.fullScreenAdCallback){
  113. self.fullScreenAdCallback(self.isAdReadly);
  114. }
  115. }
  116. /**
  117. * 模板插屏半屏广告视图展示成功回调
  118. * 模板插屏半屏广告展示成功回调该函数
  119. */
  120. - (void)expressInterstitialDidPresentScreen:(GDTExpressInterstitialAd *)unifiedInterstitial
  121. {
  122. NSLog(@"%s",__FUNCTION__);
  123. NSLog(@"%@",[NSString stringWithFormat:@"插屏状态:%@",@"Success Presented." ]);
  124. self.isAdReadly = YES;
  125. if(self.fullScreenAdCallback){
  126. self.fullScreenAdCallback(self.isAdReadly);
  127. }
  128. }
  129. /**
  130. * 模板插屏半屏广告展示结束回调
  131. * 模板插屏半屏广告展示结束回调该函数
  132. */
  133. - (void)expressInterstitialDidDismissScreen:(GDTExpressInterstitialAd *)unifiedInterstitial
  134. {
  135. NSLog(@"%s",__FUNCTION__);
  136. NSLog(@"%@",[NSString stringWithFormat:@"插屏状态:%@",@"Finish Presented." ]);
  137. self.interstitial = nil;
  138. }
  139. /**
  140. * 当点击下载应用时会调用系统程序打开
  141. */
  142. - (void)expressInterstitialWillLeaveApplication:(GDTExpressInterstitialAd *)unifiedInterstitial
  143. {
  144. NSLog(@"%s",__FUNCTION__);
  145. NSLog(@"%@",[NSString stringWithFormat:@"插屏状态:%@",@"Application enter background." ]);
  146. }
  147. /**
  148. * 模板插屏半屏广告曝光回调
  149. */
  150. - (void)expressInterstitialWillExposure:(GDTExpressInterstitialAd *)expressInterstitial
  151. {
  152. NSLog(@"%s",__FUNCTION__);
  153. }
  154. /**
  155. * 模板插屏半屏广告点击回调
  156. */
  157. - (void)expressInterstitialClicked:(GDTExpressInterstitialAd *)unifiedInterstitial {
  158. NSLog(@"%s",__FUNCTION__);
  159. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  160. }
  161. /**
  162. * 点击模板插屏半屏广告以后即将弹出全屏广告页
  163. */
  164. - (void)expressInterstitialAdWillPresentFullScreenModal:(GDTExpressInterstitialAd *)unifiedInterstitial
  165. {
  166. NSLog(@"%s",__FUNCTION__);
  167. }
  168. /**
  169. * 点击模板插屏半屏广告以后弹出全屏广告页
  170. */
  171. - (void)expressInterstitialAdDidPresentFullScreenModal:(GDTExpressInterstitialAd *)unifiedInterstitial
  172. {
  173. NSLog(@"%s",__FUNCTION__);
  174. }
  175. /**
  176. * 全屏广告页将要关闭
  177. */
  178. - (void)expressInterstitialAdWillDismissFullScreenModal:(GDTExpressInterstitialAd *)unifiedInterstitial
  179. {
  180. NSLog(@"%s",__FUNCTION__);
  181. }
  182. /**
  183. * 全屏广告页被关闭
  184. */
  185. - (void)expressInterstitialAdDidDismissFullScreenModal:(GDTExpressInterstitialAd *)unifiedInterstitial
  186. {
  187. NSLog(@"%s",__FUNCTION__);
  188. }
  189. /**
  190. * 模板插屏半屏视频广告 player 播放状态更新回调
  191. */
  192. - (void)expressInterstitialAd:(GDTExpressInterstitialAd *)unifiedInterstitial playerStatusChanged:(GDTMediaPlayerStatus)status
  193. {
  194. NSLog(@"%s",__FUNCTION__);
  195. }
  196. /**
  197. * 模板插屏半屏视频广告详情页 WillPresent 回调
  198. */
  199. - (void)expressInterstitialAdViewWillPresentVideoVC:(GDTExpressInterstitialAd *)unifiedInterstitial
  200. {
  201. NSLog(@"%s",__FUNCTION__);
  202. }
  203. /**
  204. * 模板插屏半屏视频广告详情页 DidPresent 回调
  205. */
  206. - (void)expressInterstitialAdViewDidPresentVideoVC:(GDTExpressInterstitialAd *)unifiedInterstitial
  207. {
  208. NSLog(@"%s",__FUNCTION__);
  209. }
  210. /**
  211. * 模板插屏半屏视频广告详情页 WillDismiss 回调
  212. */
  213. - (void)expressInterstitialAdViewWillDismissVideoVC:(GDTExpressInterstitialAd *)unifiedInterstitial
  214. {
  215. NSLog(@"%s",__FUNCTION__);
  216. }
  217. /**
  218. * 模板插屏半屏视频广告详情页 DidDismiss 回调
  219. */
  220. - (void)expressInterstitialAdViewDidDismissVideoVC:(GDTExpressInterstitialAd *)unifiedInterstitial
  221. {
  222. NSLog(@"%s",__FUNCTION__);
  223. }
  224. @end