NYADViewManager.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // NYADViewManager.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2023/7/25.
  6. // Copyright © 2023 JCZ. All rights reserved.
  7. //
  8. #import "NYADViewManager.h"
  9. static NYADViewManager *admanger = nil;
  10. static dispatch_once_t onceToken;
  11. @interface NYADViewManager ()
  12. @property (nonatomic, strong) CXHAdSDKServiceAdView *serviceAdView;
  13. @end
  14. @implementation NYADViewManager
  15. #pragma mark - SystemMethod
  16. + (NYADViewManager *)sharedManager {
  17. dispatch_once(&onceToken, ^{
  18. admanger = [[self alloc] init];
  19. });
  20. return admanger;
  21. }
  22. - (instancetype)init {
  23. self = [super init];
  24. if (self) {
  25. [self initBaseData];
  26. }
  27. return self;
  28. }
  29. - (void)dealloc {
  30. }
  31. #pragma mark - PublicMethods
  32. // 配置广告SDK
  33. - (void)ny_configureADSuiSDK {
  34. if ([NY_AD_MANAGER isFirstAppLoad]) {
  35. [NY_AD_MANAGER requestAgreePrivacy];
  36. } else {
  37. [NY_AD_MANAGER initCXHAdSDK];
  38. }
  39. }
  40. - (void)writeAppLoad {
  41. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  42. [userDefault setObject:@"yes" forKey:@"isCHXFirstLoad"];
  43. [userDefault synchronize];
  44. }
  45. - (BOOL)isFirstAppLoad {
  46. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  47. if ([[userDefault objectForKey:@"isCHXFirstLoad"] isEqualToString:@"yes"]) {
  48. return NO;
  49. }
  50. return YES;
  51. }
  52. - (void)loadAdWithPosid:(NSString *)posid customView:(UIView *)customView {
  53. if (_serviceAdView) {
  54. [_serviceAdView removeFromSuperview];
  55. _serviceAdView = nil;
  56. }
  57. CGFloat height = customView.width;
  58. CGFloat width = customView.width;
  59. // CGFloat viewY = (customView.height-width);
  60. // 1 初始化banner视图
  61. UIViewController *current_VC = RQControllerHelper.currentViewController;
  62. _serviceAdView = [[CXHAdSDKServiceAdView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
  63. _serviceAdView.delegate = self;
  64. _serviceAdView.controller = current_VC;
  65. _serviceAdView.refershTime = 35;
  66. _serviceAdView.posId = posid;
  67. _serviceAdView.backgroundColor = [UIColor whiteColor];
  68. // 2 添加到父视图上
  69. _serviceAdView.frame = CGRectMake(0, 0, width, height);
  70. _serviceAdView.centerY = customView.centerY;
  71. [customView addSubview:_serviceAdView];
  72. // 3 加载并显示广告 注意: 请确保banner视图显示在屏幕内的时候,调用load方法
  73. [_serviceAdView loadAndShow];
  74. }
  75. - (void)requestAgreePrivacy {
  76. [self writeAppLoad];
  77. [self initCXHAdSDK];
  78. }
  79. #pragma mark - PrivateMethods
  80. - (void)initCXHAdSDK {
  81. [CXHAdSDK setRequestDomain:@"http://ad.baihemob.com"];
  82. [CXHAdSDK setLogLevel:CXHSDKLogLevelDebug];
  83. NSString *CXHAppid = @"458110";
  84. [CXHAdSDK initWithAppId:CXHAppid completionBlock:^(NSError * _Nullable error) {
  85. if (error) {
  86. NSLog(@"CXH初始化失败:%@", error);
  87. }
  88. }];
  89. }
  90. - (void)initBaseData {
  91. [[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] map:^id(NSNotification *value) {
  92. return value.object;
  93. }] distinctUntilChanged] subscribeNext:^(id x) {
  94. NSLog(@"%@",@"APP进入后台");
  95. }];
  96. }
  97. #pragma mark - CXHSDKServiceAdViewDelegate
  98. /**
  99. 广告获取成功
  100. @param serviceAdView banner实例
  101. */
  102. - (void)CXH_serviceAdViewDidReceived:(CXHAdSDKServiceAdView *)serviceAdView{
  103. }
  104. /**
  105. 广告拉取失败
  106. @param serviceAdView banner实例
  107. @param error 错误描述
  108. */
  109. - (void)CXH_serviceAdViewFailToReceived:(CXHAdSDKServiceAdView *)serviceAdView error:(NSError *)error{
  110. [_serviceAdView removeFromSuperview];
  111. _serviceAdView = nil;
  112. }
  113. /**
  114. 广告点击
  115. @param serviceAdView 广告实例
  116. @param loadingPageURL 广告落地页地址,当渠道为bwt,并且customLoadingPage为YES时有值
  117. */
  118. - (void)CXH_serviceAdViewClicked:(CXHAdSDKServiceAdView *)serviceAdView loadingPageURL:(NSString *)loadingPageURL{
  119. }
  120. /**
  121. 广告关闭
  122. @param serviceAdView 广告实例
  123. */
  124. - (void)CXH_serviceAdViewClose:(CXHAdSDKServiceAdView *)serviceAdView{
  125. dispatch_async(dispatch_get_main_queue(), ^{
  126. _serviceAdView = nil;
  127. });
  128. }
  129. /**
  130. 广告展示
  131. @param serviceAdView 广告实例
  132. */
  133. - (void)CXH_serviceAdViewExposure:(CXHAdSDKServiceAdView *)serviceAdView{
  134. }
  135. /**
  136. 关闭落地页
  137. @param serviceAdView 广告实例
  138. */
  139. - (void)CXH_serviceAdViewCloseLandingPage:(CXHAdSDKServiceAdView *)serviceAdView{
  140. }
  141. @end