123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- //
- // FqBUDNativeExpressBanner.m
- // XenonSDK
- //
- // Created by fq on 2021/4/22.
- // Copyright © 2021 SAGESSE. All rights reserved.
- //
- #import "FqBUDNativeExpressBanner.h"
- #import "GMTools.h"
- #import "XSNetwork.h"
- @interface FqBUDNativeExpressBanner()<BUNativeExpressBannerViewDelegate>
- @property(nonatomic, strong) BUNativeExpressBannerView *bannerView;
- @property(nonatomic, copy) NSDictionary *sizeDcit;
- @property(nonatomic,assign)CGFloat x;
- @property(nonatomic,assign)CGFloat y;
- @property(nonatomic,assign)BOOL isResourceReadly;
- @property(nonatomic,assign)BOOL switchOn; //轮播开关
- @property(nonatomic,strong)void(^nativeBannerCallback)(BOOL);
- @end
- @implementation FqBUDNativeExpressBanner
- -(instancetype)init{
- if (self = [super init]) {
- self.agentName = @"ChuanSANJIA"; //平台名称
- self.adUnitId = @"banner";
- self.type = @"banner"; //广告类型
- NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
- self.adUnitId = [infos objectForKey:@"csj-banner-unitId"]; //广告组ID
- if ([[infos allKeys]containsObject:@"banner-carousel"]) {
- int flag = (int)[infos objectForKey:@"banner-carousel"]; //轮播开关 YES:开,NO:关
- if (flag == 1) {
- self.switchOn = YES;
- }else if (flag == 0){
- self.switchOn = NO;
- }
- }
- self.isResourceReadly = NO;
- //ps:横幅广告不做预加载.
- }
- return self;
- }
- //横幅广告是否准备好
- -(BOOL)isAdReady {
-
- return self.isResourceReadly;
- }
- //关闭横幅广告
- -(void)closeBanner:(NSString *)adName {
- if ([adName isEqualToString:self.adId]) {
- if (self.bannerView) {
- [self.bannerView removeFromSuperview];
- self.bannerView = nil;
- }
- }
- }
- //打开横幅广告
- -(void)openNativeExpressBannerAd:(NSString *)adName WithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)w Height:(CGFloat)h callback:(void (^)(BOOL))callback {
- self.adId = adName;
- self.nativeBannerCallback = callback;
- //广告的x,y位置坐标
- self.x = x;
- self.y = y;
- dispatch_async(dispatch_get_main_queue(), ^{
- self.sizeDcit = @{self.adUnitId : [NSValue valueWithCGSize:CGSizeMake(w, h)]};
- [self loadBannerWithSlotID:self.adUnitId];
- });
- }
- /***important:
- 广告加载成功的时候,会立即渲染WKWebView。
- 如果想预加载的话,建议一次最多预加载三个广告,如果超过3个会很大概率导致WKWebview渲染失败。
- */
- - (void)loadBannerWithSlotID:(NSString *)slotID {
-
- [self.bannerView removeFromSuperview];
-
- UIWindow *window = nil;
- if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]) {
- window = [[UIApplication sharedApplication].delegate window];
- }
- if (![window isKindOfClass:[UIView class]]) {
- window = [UIApplication sharedApplication].keyWindow;
- }
- if (!window) {
- window = [[UIApplication sharedApplication].windows objectAtIndex:0];
- }
- CGFloat bottom = 0.0;
- if (@available(iOS 11.0, *)) {
- bottom = window.safeAreaInsets.bottom;
- } else {
- // Fallback on earlier versions
- }
-
- NSValue *sizeValue = [self.sizeDcit objectForKey:slotID];
- CGSize size = [sizeValue CGSizeValue];
- // native_banner_ID
- NSString *realSlotId = slotID;
- // important: 升级的用户请注意,初始化方法去掉了imgSize参数
- UIWindow *keyWindow = [[UIApplication sharedApplication].delegate window];
- if (self.switchOn) {
- //interval 轮播时间
- self.bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:realSlotId rootViewController:keyWindow.rootViewController adSize:size interval:30];
- self.bannerView.layer.masksToBounds = YES;
- } else {
- self.bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:realSlotId rootViewController:keyWindow.rootViewController adSize:size];
- }
-
-
- //CGFloat WIDTH = [UIScreen mainScreen].bounds.size.width;
- //CGFloat HEIGHT = [UIScreen mainScreen].bounds.size.height;
- //self.bannerView.frame = CGRectMake((WIDTH-size.width)/2.0, HEIGHT-size.height-bottom, size.width, size.height);
- self.bannerView.frame = CGRectMake(self.x, self.y, size.width, size.height);
-
- // 不支持中途更改代理,中途更改代理会导致接收不到广告相关回调,如若存在中途更改代理场景,需自行处理相关逻辑,确保广告相关回调正常执行。
- self.bannerView.delegate = self;
- [self.bannerView loadAdData];
-
-
- //数据上报后台服务器
- [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- - (void)showBanner {
- UIViewController *vc = [GMTools getViewControl];
- [vc.view addSubview:self.bannerView];
-
- //数据上报后台服务器
- [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- #pragma BUNativeExpressBannerViewDelegate
- - (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
-
- NSLog(@"[穿山甲]:横幅广告素材加载成功");
-
- }
- - (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *)error {
- [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
-
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
-
- self.isResourceReadly = NO;
- if (self.nativeBannerCallback) {
- self.nativeBannerCallback(self.isResourceReadly);
- }
-
- }
- - (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
-
- //在此回调方法中进行广告的展示,可保证播放流畅和展示流畅,用户体验更好。
- self.isResourceReadly = YES;
- if (self.nativeBannerCallback) {
- self.nativeBannerCallback(self.isResourceReadly);
- }
-
- //显示广告
- [self showBanner];
-
- }
- - (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError *)error {
- [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
-
- //广告回调
- self.isResourceReadly = NO;
- if (self.nativeBannerCallback) {
- self.nativeBannerCallback(self.isResourceReadly);
- }
-
- }
- - (void)nativeExpressBannerAdViewWillBecomVisible:(BUNativeExpressBannerView *)bannerAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
- }
- - (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
-
- //数据上报后台服务器
- [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- - (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView dislikeWithReason:(NSArray<BUDislikeWords *> *)filterwords {
- //关闭"X"广告回调
-
- [UIView animateWithDuration:0.25 animations:^{
- bannerAdView.alpha = 0;
- } completion:^(BOOL finished) {
- [bannerAdView removeFromSuperview];
- self.bannerView = nil;
- }];
- [self pbud_logWithSEL:_cmd msg:@""];
-
- //数据上报后台服务器
- [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- - (void)nativeExpressBannerAdViewDidCloseOtherController:(BUNativeExpressBannerView *)bannerAdView interactionType:(BUInteractionType)interactionType {
-
- NSString *str;
- if (interactionType == BUInteractionTypePage) {
- str = @"ladingpage";
- } else if (interactionType == BUInteractionTypeVideoAdDetail) {
- str = @"videoDetail";
- } else {
- str = @"appstoreInApp";
- }
- [self pbud_logWithSEL:_cmd msg:str];
- }
- - (void)nativeExpressBannerAdViewDidRemoved:(BUNativeExpressBannerView *)nativeExpressAdView {
- //【重要】若开发者收到此回调,代表穿山甲会主动关闭掉广告,广告移除后需要开发者对界面进行适配
-
- [UIView animateWithDuration:0.25 animations:^{
- nativeExpressAdView.alpha = 0;
- } completion:^(BOOL finished) {
- [nativeExpressAdView removeFromSuperview];
- self.bannerView = nil;
- }];
- }
- #pragma mark - Log
- - (void)pbud_logWithSEL:(SEL)sel msg:(NSString *)msg {
- NSLog(@"SDKDemoDelegate BUNativeExpressBannerView In VC (%@) extraMsg:%@", NSStringFromSelector(sel), msg);
- }
- @end
|