ISAdBanner.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // ISAdBanner.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2020/12/25.
  6. // Copyright © 2020 SAGESSE. All rights reserved.
  7. //
  8. #import "ISAdBanner.h"
  9. #import <IronSource/IronSource.h>
  10. #import "XSNetwork.h"
  11. @interface ISAdBanner()<ISBannerDelegate>
  12. //是否加载好
  13. @property (nonatomic, assign)BOOL ready;
  14. //是否需要显示
  15. @property (nonatomic, assign)BOOL isShow;
  16. @property (nonatomic, strong)ISBannerView *bannerView;
  17. @end
  18. @implementation ISAdBanner
  19. - (instancetype)init
  20. {
  21. self = [super init];
  22. if (self) {
  23. self.isShow = NO;
  24. self.ready = NO;
  25. self.adId = @""; //广告位名称
  26. self.agentName = @"ironsource"; //平台名称
  27. self.medium = @""; //中介
  28. self.adUnitId = @"Banner"; //原平台单元id
  29. self.type = @"Banner"; //广告类型
  30. self.unitAdId = @""; //广告组ID
  31. self.errorMsg = @""; //错误信息
  32. //init
  33. [IronSource setBannerDelegate:self];
  34. }
  35. return self;
  36. }
  37. -(BOOL)isReady {
  38. return self.ready;
  39. }
  40. //显示
  41. - (void)play:(NSString *)adId callback:(void (^)(int))back {
  42. if (self.isShow) {
  43. return;
  44. }
  45. self.adId = adId;
  46. self.isShow = YES;
  47. if (self.bannerView != nil) {
  48. UIWindow *window = [[UIApplication sharedApplication].delegate window];
  49. if (window == [self currentWindow]) {
  50. [window addSubview:self.bannerView];
  51. }
  52. }else{
  53. UIWindow *window = [self currentWindow];
  54. //
  55. [IronSource loadBannerWithViewController:window.rootViewController size:ISBannerSize_BANNER];
  56. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  57. }
  58. }
  59. //关闭
  60. -(void)close {
  61. self.isShow = NO;
  62. if (self.bannerView != nil) {
  63. [self.bannerView removeFromSuperview];
  64. [self destroyBanner];
  65. }
  66. }
  67. //销毁
  68. - (void)destroyBanner {
  69. dispatch_async(dispatch_get_main_queue(), ^{
  70. if (self.bannerView) {
  71. [IronSource destroyBanner:self.bannerView];
  72. self.bannerView = nil;
  73. }
  74. });
  75. }
  76. #pragma mark - ISBannerDelegate
  77. /**
  78. Called after a banner ad has been successfully loaded
  79. */
  80. - (void)bannerDidLoad:(ISBannerView *)bannerView {
  81. NSLog(@"%s",__PRETTY_FUNCTION__);
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. self.bannerView = bannerView;
  84. self.ready = YES;
  85. UIWindow *window = [self currentWindow];
  86. if (window) {
  87. UIViewController *viewController = window.rootViewController;
  88. if (@available(iOS 11.0, *)) {
  89. [self.bannerView setCenter:CGPointMake(viewController.view.center.x, viewController.view.frame.size.height -(self.bannerView.frame.size.height/2.0) - viewController.view.safeAreaInsets.bottom)];
  90. }else{
  91. [self.bannerView setCenter:CGPointMake(viewController.view.center.x, viewController.view.frame.size.height -(self.bannerView.frame.size.height/2.0))];
  92. }
  93. if (self.isShow) {
  94. [window addSubview:self.bannerView];
  95. }
  96. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  97. }
  98. });
  99. }
  100. /**
  101. Called after a banner has attempted to load an ad but failed.
  102. @param error The reason for the error
  103. */
  104. - (void)bannerDidFailToLoadWithError:(NSError *)error {
  105. NSLog(@"%s",__PRETTY_FUNCTION__);
  106. self.ready = NO;
  107. //收集错误日志上报服务器.
  108. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  109. }
  110. /**
  111. Called after a banner has been clicked.
  112. */
  113. - (void)didClickBanner {
  114. NSLog(@"%s",__PRETTY_FUNCTION__);
  115. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  116. }
  117. /**
  118. Called when a banner is about to present a full screen content.
  119. */
  120. - (void)bannerWillPresentScreen {
  121. NSLog(@"%s",__PRETTY_FUNCTION__);
  122. }
  123. /**
  124. Called after a full screen content has been dismissed.
  125. */
  126. - (void)bannerDidDismissScreen {
  127. NSLog(@"%s",__PRETTY_FUNCTION__);
  128. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  129. }
  130. /**
  131. Called when a user would be taken out of the application context.
  132. */
  133. - (void)bannerWillLeaveApplication {
  134. NSLog(@"%s",__PRETTY_FUNCTION__);
  135. }
  136. @end