SPSBOpenADManager.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // SPSBOpenADManager.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/26.
  6. //
  7. #import "SPSBOpenADManager.h"
  8. #import <NSObject+JXHNotification.h>
  9. #import "AppDelegate+SPSBRootViewController.h"
  10. #import <JXHSystemShortcut.h>
  11. #import <AdSupport/AdSupport.h>
  12. #import <BUAdSDK/BUAdSDK.h>
  13. #import "SPSBUIGeneralHeader.h"
  14. @interface SPSBOpenADManager ()<BUSplashAdDelegate> {
  15. UIButton *_adSkipButton;
  16. BUSplashAd *_ad;
  17. UIView *_adBottomView;
  18. }
  19. @end
  20. @implementation SPSBOpenADManager
  21. + (instancetype)alloc {
  22. NSAssert(![self isMemberOfClass:[SPSBOpenADManager class]], @"SPSBOpenADManager is singleton, you should not instantiate it directly.");
  23. return [super alloc];
  24. }
  25. + (SPSBOpenADManager *)shareManager {
  26. static SPSBOpenADManager *manager = nil;
  27. static dispatch_once_t once;
  28. dispatch_once(&once, ^{
  29. manager = [[super allocWithZone:nil] init];
  30. });
  31. return manager;
  32. }
  33. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  34. return [self shareManager];
  35. }
  36. - (void)dealloc {
  37. [self removeNotificationName:UIApplicationWillEnterForegroundNotification];
  38. }
  39. - (void)showAD {
  40. [self setOpenAD];
  41. [self addNotificationName:UIApplicationWillEnterForegroundNotification block:^(id object, NSDictionary *userInfo) {
  42. [jxh_appDelegate() showHomePage];
  43. }];
  44. }
  45. - (void)setOpenAD {
  46. // _ad = [[BUSplashView alloc] initWithSlotID:@"887888474" frame:CGRectMake(0, 0, jxh_screenWidth(), jxh_screenHeight() - SPSBStarPageBottomViewHeight - jxh_safeInsets(spsb_keyWindow()).bottom)];
  47. _ad = [[BUSplashAd alloc] initWithSlotID:@"887888474" adSize:(CGSize){jxh_screenWidth(), jxh_screenHeight() - SPSBStarPageBottomViewHeight - jxh_safeInsets(spsb_keyWindow()).bottom}];
  48. _ad.splashView.backgroundColor = spsb_FFFFFF_color(1.f);
  49. _ad.hideSkipButton = true;
  50. _ad.delegate = self;
  51. _ad.tolerateTimeout = 3;
  52. [_ad loadAdData];
  53. }
  54. - (void)splashAdDidLoad:(BUSplashAd *)splashAd {
  55. [_ad showSplashViewInRootViewController:spsb_homeVC()];
  56. _adSkipButton = [UIButton convenienceWithFont:spsb_font(14) target:self action:@selector(skipAd)];
  57. [_adSkipButton setTitle:@"跳过" titleColor:spsb_FFFFFF_color(1.f) state:JXHButtonControlStateNormal];
  58. _adSkipButton.backgroundColor = spsb_000000_color(0.5);
  59. [_adSkipButton setLayerCornerRadius:15 clipToBounds:false];
  60. [_ad.splashView addSubview:_adSkipButton];
  61. [_adSkipButton makeConstraints:^(JXHConstraintMaker *make) {
  62. make.trailing.equalTo(-16);
  63. make.top.equalTo(50);
  64. make.size.equalTo(CGSizeMake(50, 30));
  65. }];
  66. _adBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, jxh_screenHeight() - SPSBStarPageBottomViewHeight - jxh_safeInsets(spsb_keyWindow()).bottom, jxh_screenWidth(), SPSBStarPageBottomViewHeight + jxh_safeInsets(spsb_keyWindow()).bottom)];
  67. _adBottomView.backgroundColor = spsb_FFFFFF_color(1.f);
  68. [_ad.splashView addSubview:_adBottomView];
  69. UIView *contentView = [[UIView alloc] init];
  70. contentView.backgroundColor = [UIColor whiteColor];
  71. [_adBottomView addSubview:contentView];
  72. [contentView makeConstraints:^(JXHConstraintMaker *make) {
  73. make.centerX.equalTo(self->_adBottomView);
  74. make.height.equalTo(106);
  75. make.top.equalTo(0);
  76. }];
  77. UIImageView *logo = [[UIImageView alloc] initWithImage:jxh_getImage(ad_logo)];
  78. [contentView addSubview:logo];
  79. [logo makeConstraints:^(JXHConstraintMaker *make) {
  80. make.leading.equalTo(0);
  81. make.centerY.equalTo(contentView);
  82. }];
  83. UIImageView *font = [[UIImageView alloc] initWithImage:jxh_getImage(ad_logo_font)];
  84. [contentView addSubview:font];
  85. [font makeConstraints:^(JXHConstraintMaker *make) {
  86. make.leading.equalTo(logo.trailing).offset(10);
  87. make.trailing.equalTo(0);
  88. make.centerY.equalTo(contentView);
  89. }];
  90. jxh_delayAction(1, dispatch_get_main_queue(), ^{
  91. [jxh_appDelegate() changeTabBarType:SPSBTabBarTypeHome];
  92. [self performSelector:@selector(skipAd) withObject:nil afterDelay:3];
  93. });
  94. }
  95. - (void)closeAD {
  96. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  97. if (_ad) {
  98. [_ad removeSplashView];
  99. _ad = nil;
  100. [_adBottomView removeFromSuperview];
  101. [_adSkipButton removeFromSuperview];
  102. _adSkipButton = nil;
  103. _adBottomView = nil;
  104. }
  105. }
  106. - (void)skipAd {
  107. [self closeAD];
  108. [jxh_appDelegate() showHomePage];
  109. }
  110. #pragma mark - BUSplashAdDelegate
  111. /// This method is called when material load successful
  112. - (void)splashAdLoadSuccess:(BUSplashAd *)splashAd {
  113. debugLog(@"----------获取广告成功");
  114. [self splashAdDidLoad:_ad];
  115. }
  116. /// This method is called when material load failed
  117. - (void)splashAdLoadFail:(BUSplashAd *)splashAd error:(BUAdError *_Nullable)error {
  118. debugLog(@"-=-=-=-=-=--获取广告失败");
  119. [self closeAD];
  120. [jxh_appDelegate() showHomePage];
  121. }
  122. /// This method is called when splash view render successful
  123. - (void)splashAdRenderSuccess:(BUSplashAd *)splashAd {
  124. }
  125. /// This method is called when splash view render failed
  126. - (void)splashAdRenderFail:(BUSplashAd *)splashAd error:(BUAdError *_Nullable)error {
  127. }
  128. /// This method is called when splash view will show
  129. - (void)splashAdWillShow:(BUSplashAd *)splashAd {
  130. }
  131. /// This method is called when splash view did show
  132. - (void)splashAdDidShow:(BUSplashAd *)splashAd {
  133. }
  134. /// This method is called when splash view is clicked.
  135. - (void)splashAdDidClick:(BUSplashAd *)splashAd {
  136. debugLog(@"-=-=-=-=-=--点击了广告");
  137. [self closeAD];
  138. // [jxh_appDelegate() showHomePage];
  139. }
  140. /// This method is called when splash view is closed.
  141. - (void)splashAdDidClose:(BUSplashAd *)splashAd closeType:(BUSplashAdCloseType)closeType {
  142. }
  143. /// This method is called when splash viewControllr is closed.
  144. - (void)splashAdViewControllerDidClose:(BUSplashAd *)splashAd {
  145. }
  146. /**
  147. This method is called when another controller has been closed.
  148. @param interactionType : open appstore in app or open the webpage or view video ad details page.
  149. */
  150. - (void)splashDidCloseOtherController:(BUSplashAd *)splashAd interactionType:(BUInteractionType)interactionType {
  151. debugLog(@"-=-=-=-=-=--关闭广告详情");
  152. [jxh_appDelegate() showHomePage];
  153. }
  154. /// This method is called when when video ad play completed or an error occurred.
  155. - (void)splashVideoAdDidPlayFinish:(BUSplashAd *)splashAd didFailWithError:(NSError *)error {
  156. }
  157. @end
  158. SPSBOpenADManager *spsb_openADManager(void) {
  159. return [SPSBOpenADManager shareManager];
  160. }