FqBUDExpressRewardedVideo.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // FqBUDExpressRewardedVideo.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/4/22.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqBUDExpressRewardedVideo.h"
  9. #import "GMTools.h"
  10. #import "XSNetwork.h"
  11. @interface FqBUDExpressRewardedVideo()<BUNativeExpressRewardedVideoAdDelegate>
  12. @property (nonatomic, strong) BUNativeExpressRewardedVideoAd *rewardedvideoAd;
  13. @property(nonatomic,assign)BOOL isVideoReadly;
  14. @property(nonatomic,assign)BOOL giveReward; //奖励回调
  15. @property(nonatomic,strong)void(^RewardedVideoCallback)(BOOL,BOOL);
  16. @end
  17. @implementation FqBUDExpressRewardedVideo
  18. -(instancetype)init{
  19. if (self = [super init]) {
  20. self.agentName = @"ChuanSANJIA"; //平台名称
  21. self.adUnitId = @"Rewarded";
  22. self.type = @"Rewarded"; //广告类型
  23. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  24. self.adUnitId = [infos objectForKey:@"csj-rewarded-unitId"]; //广告组ID
  25. self.isVideoReadly = NO;
  26. self.giveReward = NO;
  27. //
  28. if (self.adUnitId.length == 0) {
  29. NSLog(@"【穿山甲广告】,error=视频激励广告广告位id为空!!!");
  30. }
  31. BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
  32. self.rewardedvideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:self.adUnitId rewardedVideoModel:model];
  33. // 不支持中途更改代理,中途更改代理会导致接收不到广告相关回调,如若存在中途更改代理场景,需自行处理相关逻辑,确保广告相关回调正常执行。
  34. self.rewardedvideoAd.delegate = self;
  35. //[self loadRewardVideoAdWithSlotID:slotId];
  36. }
  37. return self;
  38. }
  39. //广告是否准备好
  40. -(BOOL)isAdReady {
  41. return self.isVideoReadly;
  42. }
  43. //打开广告
  44. -(void)openRewardedVideoAd:(NSString *)adName callback:(void (^)(BOOL,BOOL))callback {
  45. self.adId = adName;
  46. self.RewardedVideoCallback = callback;
  47. NSString *slotId = self.adUnitId;
  48. [self loadRewardVideoAdWithSlotID:slotId];
  49. }
  50. //加载广告
  51. - (void)loadRewardVideoAdWithSlotID:(NSString *)slotID {
  52. // BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
  53. // self.rewardedvideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:slotID rewardedVideoModel:model];
  54. // // 不支持中途更改代理,中途更改代理会导致接收不到广告相关回调,如若存在中途更改代理场景,需自行处理相关逻辑,确保广告相关回调正常执行。
  55. // self.rewardedvideoAd.delegate = self;
  56. [self.rewardedvideoAd loadAdData];
  57. //上报数据
  58. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  59. }
  60. // important: show的时候会进行WKWebview的渲染,建议一次最多展示三个广告,如果超过3个会很大概率导致WKWebview渲染失败。当然一般情况下激励视频一次只会show一个
  61. - (void)showRewardVideoAd {
  62. if (self.rewardedvideoAd) {
  63. UIWindow *keyWindow = [GMTools getKeyWindow];
  64. [self.rewardedvideoAd showAdFromRootViewController:keyWindow.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. }
  69. #pragma mark - BUNativeExpressRewardedVideoAdDelegate
  70. - (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  71. [self pbud_logWithSEL:_cmd msg:@""];
  72. NSLog(@"[穿山甲]:视频激励广告素材加载成功");
  73. }
  74. - (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
  75. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"%@", error]];
  76. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  77. self.isVideoReadly = NO;
  78. self.giveReward = NO;
  79. if (self.RewardedVideoCallback) {
  80. self.RewardedVideoCallback(self.isVideoReadly,self.giveReward);
  81. }
  82. }
  83. - (void)nativeExpressRewardedVideoAdCallback:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd withType:(BUNativeExpressRewardedVideoAdType)nativeExpressVideoType{
  84. [self pbud_logWithSEL:_cmd msg:@""];
  85. }
  86. - (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  87. [self pbud_logWithSEL:_cmd msg:@""];
  88. self.isVideoReadly = YES;
  89. self.giveReward = NO;
  90. //在此回调方法后进行广告的展示,可保证播放流畅和展示流畅,用户体验更好。
  91. [self showRewardVideoAd];
  92. if (self.RewardedVideoCallback) {
  93. self.RewardedVideoCallback(self.isVideoReadly,self.giveReward);
  94. }
  95. }
  96. - (void)nativeExpressRewardedVideoAdViewRenderSuccess:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  97. [self pbud_logWithSEL:_cmd msg:@""];
  98. }
  99. - (void)nativeExpressRewardedVideoAdViewRenderFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error {
  100. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"%@", error]];
  101. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  102. self.isVideoReadly = NO;
  103. self.giveReward = NO;
  104. if (self.RewardedVideoCallback) {
  105. self.RewardedVideoCallback(self.isVideoReadly,self.giveReward);
  106. }
  107. }
  108. - (void)nativeExpressRewardedVideoAdWillVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  109. [self pbud_logWithSEL:_cmd msg:@""];
  110. }
  111. - (void)nativeExpressRewardedVideoAdDidVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  112. [self pbud_logWithSEL:_cmd msg:@""];
  113. }
  114. - (void)nativeExpressRewardedVideoAdWillClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  115. [self pbud_logWithSEL:_cmd msg:@""];
  116. }
  117. - (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  118. [self pbud_logWithSEL:_cmd msg:@""];
  119. //上报数据
  120. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  121. //加载广告回调
  122. if (self.RewardedVideoCallback) {
  123. self.RewardedVideoCallback(self.isVideoReadly,self.giveReward);
  124. self.giveReward = NO;
  125. }
  126. //在此回调方法中可进行广告的置空操作.
  127. //同一次请求的广告最多只能计一次展示,重复的展示会被系统过滤。开发者在用户观看完广告后在nativeExpressRewardedVideoAdDidClose回调里将原来的广告对象置为nil 保证广告对象为新的请求对象
  128. self.rewardedvideoAd = nil;
  129. }
  130. - (void)nativeExpressRewardedVideoAdDidClick:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  131. [self pbud_logWithSEL:_cmd msg:@""];
  132. //上报数据
  133. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  134. }
  135. - (void)nativeExpressRewardedVideoAdDidClickSkip:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
  136. [self pbud_logWithSEL:_cmd msg:@""];
  137. }
  138. - (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
  139. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"%@", error]];
  140. }
  141. - (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify {
  142. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"verify:%@ rewardName:%@ rewardMount:%ld",verify?@"true":@"false",rewardedVideoAd.rewardedVideoModel.rewardName,(long)rewardedVideoAd.rewardedVideoModel.rewardAmount]];
  143. self.giveReward = verify;
  144. //视频完整播放完成,发放奖励.
  145. if (self.RewardedVideoCallback) {
  146. self.RewardedVideoCallback(self.isVideoReadly,self.giveReward);
  147. }
  148. }
  149. - (void)nativeExpressRewardedVideoAdServerRewardDidFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError * _Nullable)error {
  150. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"rewardName:%@ rewardMount:%ld error:%@",rewardedVideoAd.rewardedVideoModel.rewardName,(long)rewardedVideoAd.rewardedVideoModel.rewardAmount,error]];
  151. }
  152. - (void)nativeExpressRewardedVideoAdDidCloseOtherController:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd interactionType:(BUInteractionType)interactionType {
  153. NSString *str;
  154. if (interactionType == BUInteractionTypePage) {
  155. str = @"ladingpage";
  156. } else if (interactionType == BUInteractionTypeVideoAdDetail) {
  157. str = @"videoDetail";
  158. } else {
  159. str = @"appstoreInApp";
  160. }
  161. [self pbud_logWithSEL:_cmd msg:str];
  162. }
  163. #pragma mark - Log
  164. - (void)pbud_logWithSEL:(SEL)sel msg:(NSString *)msg {
  165. NSLog(@"SDKDemoDelegate BUNativeExpressRewardedVideoAd In VC (%@) extraMsg:%@", NSStringFromSelector(sel), msg);
  166. }
  167. @end