FqBUDExpressInterstitial.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // FqBUDExpressInterstitial.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/4/22.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqBUDExpressInterstitial.h"
  9. #import "XSNetwork.h"
  10. #import "GMTools.h"
  11. @interface FqBUDExpressInterstitial()<BUNativeExpresInterstitialAdDelegate>
  12. @property (nonatomic, strong) BUNativeExpressInterstitialAd *interstitialAd;
  13. @property(nonatomic,assign)BOOL isResourceReadly;
  14. @property(nonatomic,strong)void(^InterstitialCallback)(BOOL);
  15. @end
  16. @implementation FqBUDExpressInterstitial
  17. -(instancetype)init{
  18. if (self = [super init]) {
  19. self.agentName = @"ChuanSANJIA"; //平台名称
  20. self.adUnitId = @"Interstotal";
  21. self.type = @"Interstotal"; //广告类型
  22. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  23. self.adUnitId = [infos objectForKey:@"csj-Interstitial-unitId"]; //广告组ID
  24. self.isResourceReadly = NO;
  25. //[self loadInterstitialWithSlotID:self.adUnitId];
  26. }
  27. return self;
  28. }
  29. -(BOOL)isAdReady {
  30. return self.isResourceReadly;
  31. }
  32. //打开广告(未预先加载广告资源)
  33. -(void)openInterstitialAd:(NSString *)adName Width:(CGFloat)w Height:(CGFloat)h callback:(void (^)(BOOL))callback {
  34. self.adId = adName;
  35. self.InterstitialCallback = callback;
  36. if (!self.isResourceReadly) {
  37. //加载广告
  38. [self loadInterstitialWithSlotID:self.adUnitId Width:w Height:h];
  39. }
  40. }
  41. /***important:
  42. 广告加载成功的时候,会立即渲染WKWebView。
  43. 如果想预加载的话,建议一次最多预加载三个广告,如果超过3个会很大概率导致WKWebview渲染失败。
  44. */
  45. - (void)loadInterstitialWithSlotID:(NSString *)slotID Width:(CGFloat)w Height:(CGFloat)h{
  46. if (slotID.length == 0) {
  47. NSLog(@"【穿山甲广告】,error=插页广告广告位id为空!!!");
  48. return;
  49. }
  50. CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds)-40;
  51. CGFloat height = width/w*h;
  52. // important: 升级的用户请注意,初始化方法去掉了imgSize参数
  53. self.interstitialAd = [[BUNativeExpressInterstitialAd alloc] initWithSlotID:self.adUnitId adSize:CGSizeMake(width, height)];
  54. // 不支持中途更改代理,中途更改代理会导致接收不到广告相关回调,如若存在中途更改代理场景,需自行处理相关逻辑,确保广告相关回调正常执行。
  55. self.interstitialAd.delegate = self;
  56. [self.interstitialAd 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. //显示广告
  61. - (void)showInterstitial {
  62. if (self.interstitialAd) {
  63. UIWindow *keyWindow = [GMTools getKeyWindow];
  64. [self.interstitialAd 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 ---BUNativeExpresInterstitialAdDelegate
  70. - (void)nativeExpresInterstitialAdDidLoad:(BUNativeExpressInterstitialAd *)interstitialAd {
  71. [self pbud_logWithSEL:_cmd msg:@""];
  72. NSLog(@"[穿山甲]:插屏广告素材加载成功");
  73. }
  74. - (void)nativeExpresInterstitialAd:(BUNativeExpressInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
  75. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
  76. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  77. self.isResourceReadly = NO;
  78. if (self.InterstitialCallback) {
  79. self.InterstitialCallback(self.isResourceReadly);
  80. }
  81. }
  82. - (void)nativeExpresInterstitialAdRenderSuccess:(BUNativeExpressInterstitialAd *)interstitialAd {
  83. [self pbud_logWithSEL:_cmd msg:@""];
  84. //1.此回调方法中进行广告的展示,可保证播放流畅和展示流畅,用户体验更好。
  85. self.isResourceReadly = YES;
  86. [self showInterstitial];
  87. //2.广告加载回调
  88. if (self.InterstitialCallback) {
  89. self.InterstitialCallback(self.isResourceReadly);
  90. }
  91. }
  92. - (void)nativeExpresInterstitialAdRenderFail:(BUNativeExpressInterstitialAd *)interstitialAd error:(NSError *)error {
  93. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
  94. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  95. self.isResourceReadly = NO;
  96. if (self.InterstitialCallback) {
  97. self.InterstitialCallback(self.isResourceReadly);
  98. }
  99. }
  100. - (void)nativeExpresInterstitialAdWillVisible:(BUNativeExpressInterstitialAd *)interstitialAd {
  101. [self pbud_logWithSEL:_cmd msg:@""];
  102. }
  103. - (void)nativeExpresInterstitialAdDidClick:(BUNativeExpressInterstitialAd *)interstitialAd {
  104. [self pbud_logWithSEL:_cmd msg:@""];
  105. //上报数据
  106. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  107. }
  108. - (void)nativeExpresInterstitialAdWillClose:(BUNativeExpressInterstitialAd *)interstitialAd {
  109. [self pbud_logWithSEL:_cmd msg:@""];
  110. }
  111. - (void)nativeExpresInterstitialAdDidClose:(BUNativeExpressInterstitialAd *)interstitialAd {
  112. [self pbud_logWithSEL:_cmd msg:@""];
  113. //上报数据
  114. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  115. self.isResourceReadly = NO;
  116. self.interstitialAd = nil;
  117. }
  118. - (void)nativeExpresInterstitialAdDidCloseOtherController:(BUNativeExpressInterstitialAd *)interstitialAd interactionType:(BUInteractionType)interactionType {
  119. NSString *str;
  120. if (interactionType == BUInteractionTypePage) {
  121. str = @"ladingpage";
  122. } else if (interactionType == BUInteractionTypeVideoAdDetail) {
  123. str = @"videoDetail";
  124. } else {
  125. str = @"appstoreInApp";
  126. }
  127. [self pbud_logWithSEL:_cmd msg:str];
  128. }
  129. - (void)pbud_logWithSEL:(SEL)sel msg:(NSString *)msg {
  130. NSLog(@"SDKDemoDelegate BUNativeExpressInterstitialAd In VC (%@) extraMsg:%@", NSStringFromSelector(sel), msg);
  131. }
  132. @end