FqGDTRewardedVideoAd.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // FqGDTRewardedVideoAd.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/4/30.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqGDTRewardedVideoAd.h"
  9. #import "GDTRewardVideoAd.h"
  10. #import "GDTSDKConfig.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import "XSNetwork.h"
  13. #import "GMTools.h"
  14. @interface FqGDTRewardedVideoAd()<GDTRewardedVideoAdDelegate>
  15. @property (nonatomic, strong) GDTRewardVideoAd *rewardVideoAd;
  16. @property(nonatomic,assign)BOOL isAdReadly; //广告是否准备好
  17. @property(nonatomic,assign)BOOL isReward; //是否发放奖励
  18. @property(nonatomic,strong)void(^rewardVideoAdCallback)(BOOL,BOOL);
  19. @property (nonatomic, assign)BOOL videoMuted;
  20. @property (nonatomic, assign)BOOL audioSession;
  21. @end
  22. @implementation FqGDTRewardedVideoAd
  23. -(instancetype)init{
  24. if (self = [super init]) {
  25. self.agentName = @"YLH"; //平台名称
  26. self.adUnitId = @"Rewarded";
  27. self.type = @"Rewarded"; //广告类型
  28. self.isAdReadly = NO;
  29. self.isReward = NO;
  30. self.videoMuted = YES;
  31. self.audioSession = YES;
  32. //
  33. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  34. if ([infos objectForKey:@"ylh-rewarded-unitId"]) {
  35. self.adUnitId = [infos objectForKey:@"ylh-rewarded-unitId"]; //广告组ID
  36. }
  37. if (self.adUnitId.length == 0) {
  38. NSLog(@"【优量汇广告】,error=模板2视频激励广告位id为空!!!");
  39. }
  40. //预加载广告
  41. [self loadRewardedVideoAdWithPlacementId:self.adUnitId];
  42. }
  43. return self;
  44. }
  45. //打开广告
  46. -(void)openExpressRewardedVideoAd:(NSString *)adName callback:(void (^)(BOOL, BOOL))callback {
  47. self.adId = adName;
  48. self.rewardVideoAdCallback = callback;
  49. if (self.rewardVideoAd) {
  50. [self.rewardVideoAd loadAd];
  51. }
  52. }
  53. //拉取视频激励广告
  54. - (void)loadRewardedVideoAdWithPlacementId:(NSString *)placementId {
  55. if (self.rewardVideoAd) {
  56. self.rewardVideoAd.delegate = nil;
  57. }
  58. self.rewardVideoAd = [[GDTRewardVideoAd alloc] initWithPlacementId:placementId];
  59. self.rewardVideoAd.videoMuted = YES;
  60. self.rewardVideoAd.delegate = self;
  61. // [self.rewardVideoAd loadAd];
  62. //数据上报
  63. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  64. }
  65. //播放视频广告
  66. -(void)playVideoAd {
  67. if(self.rewardVideoAd.expiredTimestamp <= [[NSDate date] timeIntervalSince1970]) {
  68. NSLog(@"视频激励广告已过期,请重新拉取");
  69. return;
  70. }
  71. if (!self.rewardVideoAd.isAdValid) {
  72. NSLog(@"视频激励广告失效,请重新拉取");
  73. return;
  74. }
  75. [GDTSDKConfig enableDefaultAudioSessionSetting:self.audioSession];
  76. UIWindow *fK = [GMTools getKeyWindow];
  77. [self.rewardVideoAd showAdFromRootViewController:fK.rootViewController];
  78. if (self.audioSession) {
  79. [[AVAudioSession sharedInstance] setActive:NO error:nil];
  80. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  81. [[AVAudioSession sharedInstance] setActive:YES error:nil];
  82. }
  83. }
  84. #pragma mark - GDTRewardVideoAdDelegate
  85. - (void)gdt_rewardVideoAdDidLoad:(GDTRewardVideoAd *)rewardedVideoAd
  86. {
  87. NSLog(@"%s",__FUNCTION__);
  88. NSLog(@"%@",[NSString stringWithFormat:@"%@ 广告数据加载成功", rewardedVideoAd.adNetworkName]);
  89. NSLog(@"eCPM:%ld eCPMLevel:%@", [rewardedVideoAd eCPM], [rewardedVideoAd eCPMLevel]);
  90. NSLog(@"videoDuration :%lf rewardAdType:%ld", rewardedVideoAd.videoDuration, rewardedVideoAd.rewardAdType);
  91. self.isAdReadly = YES;
  92. if (self.rewardVideoAdCallback) {
  93. self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
  94. }
  95. [self playVideoAd];
  96. }
  97. - (void)gdt_rewardVideoAdVideoDidLoad:(GDTRewardVideoAd *)rewardedVideoAd
  98. {
  99. NSLog(@"%s",__FUNCTION__);
  100. NSLog(@"%@",[NSString stringWithFormat:@"%@ 视频文件加载成功", rewardedVideoAd.adNetworkName]);
  101. }
  102. - (void)gdt_rewardVideoAdWillVisible:(GDTRewardVideoAd *)rewardedVideoAd
  103. {
  104. NSLog(@"%s",__FUNCTION__);
  105. NSLog(@"视频播放页即将打开");
  106. }
  107. - (void)gdt_rewardVideoAdDidExposed:(GDTRewardVideoAd *)rewardedVideoAd
  108. {
  109. NSLog(@"%s",__FUNCTION__);
  110. NSLog(@"%@",[NSString stringWithFormat:@"%@ 广告已曝光", rewardedVideoAd.adNetworkName]);
  111. NSLog(@"广告已曝光");
  112. }
  113. - (void)gdt_rewardVideoAdDidClose:(GDTRewardVideoAd *)rewardedVideoAd
  114. {
  115. NSLog(@"%s",__FUNCTION__);
  116. NSLog(@"%@", [NSString stringWithFormat:@"%@ 广告已关闭", rewardedVideoAd.adNetworkName]);
  117. NSLog(@"广告已关闭");
  118. //数据上报
  119. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  120. self.rewardVideoAd = nil;
  121. }
  122. - (void)gdt_rewardVideoAdDidClicked:(GDTRewardVideoAd *)rewardedVideoAd
  123. {
  124. NSLog(@"%s",__FUNCTION__);
  125. NSLog(@"%@",[NSString stringWithFormat:@"%@ 广告已点击", rewardedVideoAd.adNetworkName]);
  126. NSLog(@"广告已点击");
  127. //数据上报
  128. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  129. }
  130. - (void)gdt_rewardVideoAd:(GDTRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error
  131. {
  132. NSLog(@"%s",__FUNCTION__);
  133. if (error.code == 4014) {
  134. NSLog(@"请拉取到广告后再调用展示接口");
  135. } else if (error.code == 4016) {
  136. NSLog(@"应用方向与广告位支持方向不一致");
  137. } else if (error.code == 5012) {
  138. NSLog(@"广告已过期");
  139. } else if (error.code == 4015) {
  140. NSLog(@"广告已经播放过,请重新拉取");
  141. } else if (error.code == 5002) {
  142. NSLog(@"视频下载失败");
  143. } else if (error.code == 5003) {
  144. NSLog(@"视频播放失败");
  145. } else if (error.code == 5004) {
  146. NSLog(@"没有合适的广告");
  147. } else if (error.code == 5013) {
  148. NSLog(@"请求太频繁,请稍后再试");
  149. } else if (error.code == 3002) {
  150. NSLog(@"网络连接超时");
  151. } else if (error.code == 5027){
  152. NSLog(@"页面加载失败");
  153. }
  154. NSLog(@"ERROR: %@", error);
  155. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  156. self.isAdReadly = NO;
  157. self.isReward = NO;
  158. if (self.rewardVideoAdCallback) {
  159. self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
  160. }
  161. }
  162. - (void)gdt_rewardVideoAdDidRewardEffective:(GDTRewardVideoAd *)rewardedVideoAd info:(NSDictionary *)info {
  163. NSLog(@"%s",__FUNCTION__);
  164. NSLog(@"播放达到激励条件 transid:%@", [info objectForKey:@"GDT_TRANS_ID"]);
  165. self.isAdReadly = YES;
  166. self.isReward = YES;
  167. if (self.rewardVideoAdCallback) {
  168. self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
  169. }
  170. }
  171. - (void)gdt_rewardVideoAdDidPlayFinish:(GDTRewardVideoAd *)rewardedVideoAd
  172. {
  173. NSLog(@"%s",__FUNCTION__);
  174. NSLog(@"视频播放结束");
  175. if (self.audioSession) {
  176. [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
  177. }
  178. }
  179. @end