FqToPONBannerAd.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // FqToPONBannerAd.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/5/13.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqToPONBannerAd.h"
  9. #import <AnyThinkBanner/AnyThinkBanner.h>
  10. #import "XSNetwork.h"
  11. #import "GMTools.h"
  12. @interface FqToPONBannerAd()<ATBannerDelegate>
  13. @property(nonatomic,strong)ATBannerView *bannerView;
  14. @property(nonatomic,assign)CGSize adSize;
  15. @property(nonatomic,assign)BOOL isAdReadly;
  16. @property(nonatomic,strong)void(^bannerAdCallback)(BOOL);
  17. @end
  18. @implementation FqToPONBannerAd
  19. -(instancetype)init{
  20. if (self = [super init]) {
  21. self.agentName = @"TopOn"; //平台名称
  22. self.adUnitId = @"Banner";
  23. self.type = @"Banner"; //广告类型
  24. self.isAdReadly = NO;
  25. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  26. if ([infos objectForKey:@"topon-banner-unitId"]) {
  27. self.adUnitId = [infos objectForKey:@"topon-banner-unitId"]; //广告组ID
  28. }
  29. //1.程序启动时,加载广告.
  30. [self loadBannerAdWithPlacementId:self.adUnitId];
  31. }
  32. return self;
  33. }
  34. //广告是否准备好
  35. -(BOOL)isAdReadly {
  36. if ([[ATAdManager sharedManager] bannerAdReadyForPlacementID:self.adUnitId]) {
  37. return YES;
  38. } else {
  39. return NO;
  40. }
  41. return NO;
  42. }
  43. //打开广告
  44. -(void)openBannerAd:(NSString *)adName callback:(void (^)(BOOL))callback {
  45. if (self.adUnitId.length == 0) {
  46. NSLog(@"【ToPon广告】,error=横幅广告广告位id为空!!!");
  47. [XSNetwork showHudFailure:@"广告位id为空!"];
  48. return;
  49. }
  50. self.adId = adName;
  51. self.bannerAdCallback = callback;
  52. //判断广告是否准备好.
  53. if([self isAdReadly]){
  54. //广告准备好,展示广告.
  55. UIViewController *vc = [GMTools getViewControl];
  56. self.bannerView = [[ATAdManager sharedManager] retrieveBannerViewForPlacementID:self.adUnitId];
  57. self.bannerView.delegate = self;
  58. self.bannerView.presentingViewController = vc;
  59. self.bannerView.translatesAutoresizingMaskIntoConstraints = NO;
  60. self.bannerView.tag = 100;
  61. [vc.view addSubview:self.bannerView];
  62. //Layour banner
  63. [vc.view addConstraint:[NSLayoutConstraint constraintWithItem:vc.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.bannerView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:.0f]];
  64. [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)]];
  65. [vc.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bannerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:_adSize.width]];
  66. [vc.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bannerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:_adSize.height]];
  67. }else{
  68. //无广告可展示,
  69. NSLog(@"Banner ad's not ready for placementID:%@", self.adUnitId);
  70. [XSNetwork showHudSuccess:@"广告正在加载中"];
  71. [self loadBannerAdWithPlacementId:self.adUnitId];
  72. }
  73. }
  74. //关闭广告
  75. -(void)closeBannerAd {
  76. [self.bannerView removeFromSuperview];
  77. self.bannerView = nil;
  78. }
  79. //加载广告资源
  80. -(void)loadBannerAdWithPlacementId:(NSString *)placementId {
  81. CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
  82. CGFloat h1 = w1/2;
  83. CGSize size = CGSizeMake(w1, h1);
  84. self.adSize = size;
  85. [[ATAdManager sharedManager] loadADWithPlacementID:self.adUnitId extra:@{kATAdLoadingExtraBannerAdSizeKey:[NSValue valueWithCGSize:size]} delegate:self];
  86. }
  87. #pragma mark - ===========ATInterstitialDelegate=============
  88. -(void) didFinishLoadingADWithPlacementID:(NSString *)placementID {
  89. NSLog(@"Banner Ad: didFinishLoadingADWithPlacementID");
  90. }
  91. -(void) didFailToLoadADWithPlacementID:(NSString* )placementID error:(NSError *)error {
  92. NSLog(@"Banner Ad: failed to load:%@", error);
  93. self.isAdReadly = NO;
  94. if(self.bannerAdCallback){
  95. self.bannerAdCallback(self.isAdReadly);
  96. }
  97. }
  98. -(void) bannerView:(ATBannerView *)bannerView didShowAdWithPlacementID:(NSString *)placementID extra:(NSDictionary*)extra {
  99. NSLog(@"FqToPONBannerAd.m::bannerView:didShowAdWithPlacementID:%@ extra: %@", placementID,extra);
  100. self.isAdReadly = YES;
  101. if(self.bannerAdCallback){
  102. self.bannerAdCallback(self.isAdReadly);
  103. }
  104. }
  105. -(void) bannerView:(ATBannerView*)bannerView didClickWithPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
  106. NSLog(@"FqToPONBannerAd.m::bannerView:didClickWithPlacementID:%@ extra: %@", placementID,extra);
  107. }
  108. -(void) bannerView:(ATBannerView*)bannerView didCloseWithPlacementID:(NSString*)placementID extra:(NSDictionary *)extra {
  109. NSLog(@"FqToPONBannerAd.m::bannerView:didCloseWithPlacementID:%@ extra: %@", placementID,extra);
  110. }
  111. -(void) bannerView:(ATBannerView *)bannerView didAutoRefreshWithPlacement:(NSString *)placementID extra:(NSDictionary*)extra {
  112. NSLog(@"FqToPONBannerAd.m::bannerView:didAutoRefreshWithPlacement:%@ extra: %@", placementID,extra);
  113. }
  114. -(void) bannerView:(ATBannerView *)bannerView failedToAutoRefreshWithPlacementID:(NSString *)placementID error:(NSError *)error {
  115. NSLog(@"FqToPONBannerAd.m::bannerView:failedToAutoRefreshWithPlacementID:%@ error:%@", placementID, error);
  116. }
  117. -(void) bannerView:(ATBannerView*)bannerView didTapCloseButtonWithPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
  118. NSLog(@"FqToPONBannerAd.m::bannerView:didTapCloseButtonWithPlacementID:%@ extra: %@", placementID,extra);
  119. //关闭广告
  120. [self closeBannerAd];
  121. }
  122. - (void)bannerView:(ATBannerView *)bannerView didDeepLinkOrJumpForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra result:(BOOL)success {
  123. }
  124. @end