123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- //
- // FqToPONBannerAd.m
- // XenonSDK
- //
- // Created by fq on 2021/5/13.
- // Copyright © 2021 SAGESSE. All rights reserved.
- //
- #import "FqToPONBannerAd.h"
- #import <AnyThinkBanner/AnyThinkBanner.h>
- #import "XSNetwork.h"
- #import "GMTools.h"
- @interface FqToPONBannerAd()<ATBannerDelegate>
- @property(nonatomic,strong)ATBannerView *bannerView;
- @property(nonatomic,assign)CGSize adSize;
- @property(nonatomic,assign)BOOL isAdReadly;
- @property(nonatomic,strong)void(^bannerAdCallback)(BOOL);
- @end
- @implementation FqToPONBannerAd
- -(instancetype)init{
- if (self = [super init]) {
- self.agentName = @"TopOn"; //平台名称
- self.adUnitId = @"Banner";
- self.type = @"Banner"; //广告类型
- self.isAdReadly = NO;
- NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
- if ([infos objectForKey:@"topon-banner-unitId"]) {
- self.adUnitId = [infos objectForKey:@"topon-banner-unitId"]; //广告组ID
- }
-
-
- //1.程序启动时,加载广告.
- [self loadBannerAdWithPlacementId:self.adUnitId];
- }
- return self;
- }
- //广告是否准备好
- -(BOOL)isAdReadly {
- if ([[ATAdManager sharedManager] bannerAdReadyForPlacementID:self.adUnitId]) {
- return YES;
- } else {
- return NO;
- }
- return NO;
- }
- //打开广告
- -(void)openBannerAd:(NSString *)adName callback:(void (^)(BOOL))callback {
- if (self.adUnitId.length == 0) {
- NSLog(@"【ToPon广告】,error=横幅广告广告位id为空!!!");
- [XSNetwork showHudFailure:@"广告位id为空!"];
- return;
- }
- self.adId = adName;
- self.bannerAdCallback = callback;
-
- //判断广告是否准备好.
- if([self isAdReadly]){
- //广告准备好,展示广告.
- UIViewController *vc = [GMTools getViewControl];
-
- self.bannerView = [[ATAdManager sharedManager] retrieveBannerViewForPlacementID:self.adUnitId];
- self.bannerView.delegate = self;
- self.bannerView.presentingViewController = vc;
- self.bannerView.translatesAutoresizingMaskIntoConstraints = NO;
- self.bannerView.tag = 100;
- [vc.view addSubview:self.bannerView];
- //Layour banner
- [vc.view addConstraint:[NSLayoutConstraint constraintWithItem:vc.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.bannerView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:.0f]];
- [vc.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bannerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:vc.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:CGRectGetHeight([UIApplication sharedApplication].statusBarFrame) + CGRectGetHeight(vc.navigationController.navigationBar.frame)]];
- [vc.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bannerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:_adSize.width]];
- [vc.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bannerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:_adSize.height]];
-
- }else{
- //无广告可展示,
- NSLog(@"Banner ad's not ready for placementID:%@", self.adUnitId);
- [XSNetwork showHudSuccess:@"广告正在加载中"];
- [self loadBannerAdWithPlacementId:self.adUnitId];
-
- }
- }
- //关闭广告
- -(void)closeBannerAd {
-
- [self.bannerView removeFromSuperview];
- self.bannerView = nil;
-
- }
- //加载广告资源
- -(void)loadBannerAdWithPlacementId:(NSString *)placementId {
-
- CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
- CGFloat h1 = w1/2;
- CGSize size = CGSizeMake(w1, h1);
- self.adSize = size;
- [[ATAdManager sharedManager] loadADWithPlacementID:self.adUnitId extra:@{kATAdLoadingExtraBannerAdSizeKey:[NSValue valueWithCGSize:size]} delegate:self];
-
- }
- #pragma mark - ===========ATInterstitialDelegate=============
- -(void) didFinishLoadingADWithPlacementID:(NSString *)placementID {
- NSLog(@"Banner Ad: didFinishLoadingADWithPlacementID");
- }
- -(void) didFailToLoadADWithPlacementID:(NSString* )placementID error:(NSError *)error {
- NSLog(@"Banner Ad: failed to load:%@", error);
-
- self.isAdReadly = NO;
- if(self.bannerAdCallback){
- self.bannerAdCallback(self.isAdReadly);
- }
-
- }
- -(void) bannerView:(ATBannerView *)bannerView didShowAdWithPlacementID:(NSString *)placementID extra:(NSDictionary*)extra {
- NSLog(@"FqToPONBannerAd.m::bannerView:didShowAdWithPlacementID:%@ extra: %@", placementID,extra);
- self.isAdReadly = YES;
- if(self.bannerAdCallback){
- self.bannerAdCallback(self.isAdReadly);
- }
-
- }
- -(void) bannerView:(ATBannerView*)bannerView didClickWithPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
- NSLog(@"FqToPONBannerAd.m::bannerView:didClickWithPlacementID:%@ extra: %@", placementID,extra);
- }
- -(void) bannerView:(ATBannerView*)bannerView didCloseWithPlacementID:(NSString*)placementID extra:(NSDictionary *)extra {
-
- NSLog(@"FqToPONBannerAd.m::bannerView:didCloseWithPlacementID:%@ extra: %@", placementID,extra);
- }
- -(void) bannerView:(ATBannerView *)bannerView didAutoRefreshWithPlacement:(NSString *)placementID extra:(NSDictionary*)extra {
- NSLog(@"FqToPONBannerAd.m::bannerView:didAutoRefreshWithPlacement:%@ extra: %@", placementID,extra);
- }
- -(void) bannerView:(ATBannerView *)bannerView failedToAutoRefreshWithPlacementID:(NSString *)placementID error:(NSError *)error {
- NSLog(@"FqToPONBannerAd.m::bannerView:failedToAutoRefreshWithPlacementID:%@ error:%@", placementID, error);
- }
- -(void) bannerView:(ATBannerView*)bannerView didTapCloseButtonWithPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
- NSLog(@"FqToPONBannerAd.m::bannerView:didTapCloseButtonWithPlacementID:%@ extra: %@", placementID,extra);
- //关闭广告
- [self closeBannerAd];
-
- }
- - (void)bannerView:(ATBannerView *)bannerView didDeepLinkOrJumpForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra result:(BOOL)success {
-
- }
- @end
|