123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- //
- // FqGDTBannerAd.m
- // XenonSDK
- //
- // Created by fq on 2021/4/30.
- // Copyright © 2021 SAGESSE. All rights reserved.
- //
- #import "FqGDTBannerAd.h"
- #import "GDTUnifiedBannerView.h"
- #import "GMTools.h"
- #import "XSNetwork.h"
- @interface FqGDTBannerAd()<GDTUnifiedBannerViewDelegate>
- @property (nonatomic, strong) GDTUnifiedBannerView *bannerView;
- @property(nonatomic,assign)BOOL isAdReadly; //广告是否准备好
- @property (nonatomic, assign)BOOL isAnimation;
- @property(nonatomic,strong)void(^bannerAdCallback)(BOOL);
- @end
- @implementation FqGDTBannerAd
- -(instancetype)init{
- if (self = [super init]) {
- self.agentName = @"YLH"; //平台名称
- self.adUnitId = @"Banner";
- self.type = @"Banner"; //广告类型
- self.isAdReadly = NO;
- self.isAnimation = NO;
-
- //
- NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
- if ([infos objectForKey:@"ylh-banner-unitId"]) {
- self.adUnitId = [infos objectForKey:@"ylh-banner-unitId"]; //广告组ID
- }
- if (self.adUnitId.length == 0) {
- NSLog(@"【优量汇广告】,error=模板2横幅广告位id为空!!!");
- }
-
- }
- return self;
- }
- #pragma mark - property getter
- -(GDTUnifiedBannerView *)bannerView {
- if (!_bannerView) {
-
- CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
- //CGRect rect = {CGPointZero, CGSizeMake(w1, 100)};
- CGFloat y = [GMTools isIPhoneXSeries]? 44.f:0.f;
- CGPoint orign = CGPointMake(0, y);
- CGRect rect = {orign, CGSizeMake(w1, 100)};
- UIWindow *fK = [GMTools getKeyWindow];
- self.bannerView = [[GDTUnifiedBannerView alloc]
- initWithFrame:rect
- placementId:self.adUnitId
- viewController:fK.rootViewController];
- self.bannerView.accessibilityIdentifier = @"banner_ad";
- self.bannerView.animated = self.isAnimation;
- self.bannerView.autoSwitchInterval = 30;
- self.bannerView.delegate = self;
-
- //数据上报
- [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- return _bannerView;
- }
- //打开广告
- -(void)openExpressBannerAd:(NSString *)adName callback:(void (^)(BOOL))callback {
- self.adId = adName;
- self.bannerAdCallback = callback;
- if (self.bannerView) {
- [self loadAdAndShowAd];
- }
- }
- //加载显示广告
- -(void)loadAdAndShowAd {
-
- if (self.bannerView.superview) {
- [self.bannerView removeFromSuperview];
- self.bannerView = nil;
- }
-
- //获取屏幕最上层VC
- UIViewController *vc = [GMTools getViewControl];
- [vc.view addSubview:self.bannerView];
- [self.bannerView loadAdAndShow];
-
- //数据上报
- [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 mark - GDTUnifiedBannerViewDelegate
- /**
- * 请求广告条数据成功后调用
- * 当接收服务器返回的广告数据成功后调用该函数
- */
- - (void)unifiedBannerViewDidLoad:(GDTUnifiedBannerView *)unifiedBannerView
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"unified banner did load");
-
- self.isAdReadly = YES;
- if(self.bannerAdCallback){
- self.bannerAdCallback(self.isAdReadly);
- }
- }
- /**
- * 请求广告条数据失败后调用
- * 当接收服务器返回的广告数据失败后调用该函数
- */
- - (void)unifiedBannerViewFailedToLoad:(GDTUnifiedBannerView *)unifiedBannerView error:(NSError *)error
- {
- NSLog(@"%s",__FUNCTION__);
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
-
- self.isAdReadly = NO;
- if(self.bannerAdCallback){
- self.bannerAdCallback(self.isAdReadly);
- }
-
- }
- /**
- * banner2.0曝光回调
- */
- - (void)unifiedBannerViewWillExpose:(nonnull GDTUnifiedBannerView *)unifiedBannerView {
- NSLog(@"%s",__FUNCTION__);
- }
- /**
- * banner2.0点击回调
- */
- - (void)unifiedBannerViewClicked:(GDTUnifiedBannerView *)unifiedBannerView
- {
- NSLog(@"%s",__FUNCTION__);
- }
- /**
- * 应用进入后台时调用
- * 当点击应用下载或者广告调用系统程序打开,应用将被自动切换到后台
- */
- - (void)unifiedBannerViewWillLeaveApplication:(GDTUnifiedBannerView *)unifiedBannerView
- {
- NSLog(@"%s",__FUNCTION__);
- }
- /**
- * 全屏广告页已经被关闭
- */
- - (void)unifiedBannerViewDidDismissFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
- {
- NSLog(@"%s",__FUNCTION__);
- }
- /**
- * 全屏广告页即将被关闭
- */
- - (void)unifiedBannerViewWillDismissFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
- {
- NSLog(@"%s",__FUNCTION__);
- }
- /**
- * banner2.0广告点击以后即将弹出全屏广告页
- */
- - (void)unifiedBannerViewWillPresentFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
- {
- NSLog(@"%s",__FUNCTION__);
- }
- /**
- * banner2.0广告点击以后弹出全屏广告页完毕
- */
- - (void)unifiedBannerViewDidPresentFullScreenModal:(GDTUnifiedBannerView *)unifiedBannerView
- {
- NSLog(@"%s",__FUNCTION__);
- }
- /**
- * banner2.0被用户关闭时调用
- */
- - (void)unifiedBannerViewWillClose:(nonnull GDTUnifiedBannerView *)unifiedBannerView {
-
- //数据上报
- [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- [self.bannerView removeFromSuperview];
- self.bannerView = nil;
- NSLog(@"%s",__FUNCTION__);
- }
- @end
|