FqBUDNativeExpressBanner.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // FqBUDNativeExpressBanner.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/4/22.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqBUDNativeExpressBanner.h"
  9. #import "GMTools.h"
  10. #import "XSNetwork.h"
  11. @interface FqBUDNativeExpressBanner()<BUNativeExpressBannerViewDelegate>
  12. @property(nonatomic, strong) BUNativeExpressBannerView *bannerView;
  13. @property(nonatomic, copy) NSDictionary *sizeDcit;
  14. @property(nonatomic,assign)CGFloat x;
  15. @property(nonatomic,assign)CGFloat y;
  16. @property(nonatomic,assign)BOOL isResourceReadly;
  17. @property(nonatomic,assign)BOOL switchOn; //轮播开关
  18. @property(nonatomic,strong)void(^nativeBannerCallback)(BOOL);
  19. @end
  20. @implementation FqBUDNativeExpressBanner
  21. -(instancetype)init{
  22. if (self = [super init]) {
  23. self.agentName = @"ChuanSANJIA"; //平台名称
  24. self.adUnitId = @"banner";
  25. self.type = @"banner"; //广告类型
  26. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  27. self.adUnitId = [infos objectForKey:@"csj-banner-unitId"]; //广告组ID
  28. if ([[infos allKeys]containsObject:@"banner-carousel"]) {
  29. int flag = (int)[infos objectForKey:@"banner-carousel"]; //轮播开关 YES:开,NO:关
  30. if (flag == 1) {
  31. self.switchOn = YES;
  32. }else if (flag == 0){
  33. self.switchOn = NO;
  34. }
  35. }
  36. self.isResourceReadly = NO;
  37. //ps:横幅广告不做预加载.
  38. }
  39. return self;
  40. }
  41. //横幅广告是否准备好
  42. -(BOOL)isAdReady {
  43. return self.isResourceReadly;
  44. }
  45. //关闭横幅广告
  46. -(void)closeBanner:(NSString *)adName {
  47. if ([adName isEqualToString:self.adId]) {
  48. if (self.bannerView) {
  49. [self.bannerView removeFromSuperview];
  50. self.bannerView = nil;
  51. }
  52. }
  53. }
  54. //打开横幅广告
  55. -(void)openNativeExpressBannerAd:(NSString *)adName WithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)w Height:(CGFloat)h callback:(void (^)(BOOL))callback {
  56. self.adId = adName;
  57. self.nativeBannerCallback = callback;
  58. //广告的x,y位置坐标
  59. self.x = x;
  60. self.y = y;
  61. dispatch_async(dispatch_get_main_queue(), ^{
  62. self.sizeDcit = @{self.adUnitId : [NSValue valueWithCGSize:CGSizeMake(w, h)]};
  63. [self loadBannerWithSlotID:self.adUnitId];
  64. });
  65. }
  66. /***important:
  67. 广告加载成功的时候,会立即渲染WKWebView。
  68. 如果想预加载的话,建议一次最多预加载三个广告,如果超过3个会很大概率导致WKWebview渲染失败。
  69. */
  70. - (void)loadBannerWithSlotID:(NSString *)slotID {
  71. [self.bannerView removeFromSuperview];
  72. UIWindow *window = nil;
  73. if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]) {
  74. window = [[UIApplication sharedApplication].delegate window];
  75. }
  76. if (![window isKindOfClass:[UIView class]]) {
  77. window = [UIApplication sharedApplication].keyWindow;
  78. }
  79. if (!window) {
  80. window = [[UIApplication sharedApplication].windows objectAtIndex:0];
  81. }
  82. CGFloat bottom = 0.0;
  83. if (@available(iOS 11.0, *)) {
  84. bottom = window.safeAreaInsets.bottom;
  85. } else {
  86. // Fallback on earlier versions
  87. }
  88. NSValue *sizeValue = [self.sizeDcit objectForKey:slotID];
  89. CGSize size = [sizeValue CGSizeValue];
  90. // native_banner_ID
  91. NSString *realSlotId = slotID;
  92. // important: 升级的用户请注意,初始化方法去掉了imgSize参数
  93. UIWindow *keyWindow = [[UIApplication sharedApplication].delegate window];
  94. if (self.switchOn) {
  95. //interval 轮播时间
  96. self.bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:realSlotId rootViewController:keyWindow.rootViewController adSize:size interval:30];
  97. self.bannerView.layer.masksToBounds = YES;
  98. } else {
  99. self.bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:realSlotId rootViewController:keyWindow.rootViewController adSize:size];
  100. }
  101. //CGFloat WIDTH = [UIScreen mainScreen].bounds.size.width;
  102. //CGFloat HEIGHT = [UIScreen mainScreen].bounds.size.height;
  103. //self.bannerView.frame = CGRectMake((WIDTH-size.width)/2.0, HEIGHT-size.height-bottom, size.width, size.height);
  104. self.bannerView.frame = CGRectMake(self.x, self.y, size.width, size.height);
  105. // 不支持中途更改代理,中途更改代理会导致接收不到广告相关回调,如若存在中途更改代理场景,需自行处理相关逻辑,确保广告相关回调正常执行。
  106. self.bannerView.delegate = self;
  107. [self.bannerView loadAdData];
  108. //数据上报后台服务器
  109. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  110. }
  111. - (void)showBanner {
  112. UIViewController *vc = [GMTools getViewControl];
  113. [vc.view addSubview:self.bannerView];
  114. //数据上报后台服务器
  115. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  116. }
  117. #pragma BUNativeExpressBannerViewDelegate
  118. - (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView {
  119. [self pbud_logWithSEL:_cmd msg:@""];
  120. NSLog(@"[穿山甲]:横幅广告素材加载成功");
  121. }
  122. - (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *)error {
  123. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
  124. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  125. self.isResourceReadly = NO;
  126. if (self.nativeBannerCallback) {
  127. self.nativeBannerCallback(self.isResourceReadly);
  128. }
  129. }
  130. - (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView {
  131. [self pbud_logWithSEL:_cmd msg:@""];
  132. //在此回调方法中进行广告的展示,可保证播放流畅和展示流畅,用户体验更好。
  133. self.isResourceReadly = YES;
  134. if (self.nativeBannerCallback) {
  135. self.nativeBannerCallback(self.isResourceReadly);
  136. }
  137. //显示广告
  138. [self showBanner];
  139. }
  140. - (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError *)error {
  141. [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
  142. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  143. //广告回调
  144. self.isResourceReadly = NO;
  145. if (self.nativeBannerCallback) {
  146. self.nativeBannerCallback(self.isResourceReadly);
  147. }
  148. }
  149. - (void)nativeExpressBannerAdViewWillBecomVisible:(BUNativeExpressBannerView *)bannerAdView {
  150. [self pbud_logWithSEL:_cmd msg:@""];
  151. }
  152. - (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView {
  153. [self pbud_logWithSEL:_cmd msg:@""];
  154. //数据上报后台服务器
  155. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  156. }
  157. - (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView dislikeWithReason:(NSArray<BUDislikeWords *> *)filterwords {
  158. //关闭"X"广告回调
  159. [UIView animateWithDuration:0.25 animations:^{
  160. bannerAdView.alpha = 0;
  161. } completion:^(BOOL finished) {
  162. [bannerAdView removeFromSuperview];
  163. self.bannerView = nil;
  164. }];
  165. [self pbud_logWithSEL:_cmd msg:@""];
  166. //数据上报后台服务器
  167. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  168. }
  169. - (void)nativeExpressBannerAdViewDidCloseOtherController:(BUNativeExpressBannerView *)bannerAdView interactionType:(BUInteractionType)interactionType {
  170. NSString *str;
  171. if (interactionType == BUInteractionTypePage) {
  172. str = @"ladingpage";
  173. } else if (interactionType == BUInteractionTypeVideoAdDetail) {
  174. str = @"videoDetail";
  175. } else {
  176. str = @"appstoreInApp";
  177. }
  178. [self pbud_logWithSEL:_cmd msg:str];
  179. }
  180. - (void)nativeExpressBannerAdViewDidRemoved:(BUNativeExpressBannerView *)nativeExpressAdView {
  181. //【重要】若开发者收到此回调,代表穿山甲会主动关闭掉广告,广告移除后需要开发者对界面进行适配
  182. [UIView animateWithDuration:0.25 animations:^{
  183. nativeExpressAdView.alpha = 0;
  184. } completion:^(BOOL finished) {
  185. [nativeExpressAdView removeFromSuperview];
  186. self.bannerView = nil;
  187. }];
  188. }
  189. #pragma mark - Log
  190. - (void)pbud_logWithSEL:(SEL)sel msg:(NSString *)msg {
  191. NSLog(@"SDKDemoDelegate BUNativeExpressBannerView In VC (%@) extraMsg:%@", NSStringFromSelector(sel), msg);
  192. }
  193. @end