// // NYADViewManager.m // jiaPei // // Created by Ning.ge on 2023/7/25. // Copyright © 2023 JCZ. All rights reserved. // #import "NYADViewManager.h" static NYADViewManager *admanger = nil; static dispatch_once_t onceToken; @interface NYADViewManager () @property (nonatomic, strong) CXHAdSDKServiceAdView *serviceAdView; @end @implementation NYADViewManager #pragma mark - SystemMethod + (NYADViewManager *)sharedManager { dispatch_once(&onceToken, ^{ admanger = [[self alloc] init]; }); return admanger; } - (instancetype)init { self = [super init]; if (self) { [self initBaseData]; } return self; } - (void)dealloc { } #pragma mark - PublicMethods // 配置广告SDK - (void)ny_configureADSuiSDK { if ([NY_AD_MANAGER isFirstAppLoad]) { [NY_AD_MANAGER requestAgreePrivacy]; } else { [NY_AD_MANAGER initCXHAdSDK]; } } - (void)writeAppLoad { NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; [userDefault setObject:@"yes" forKey:@"isCHXFirstLoad"]; [userDefault synchronize]; } - (BOOL)isFirstAppLoad { NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; if ([[userDefault objectForKey:@"isCHXFirstLoad"] isEqualToString:@"yes"]) { return NO; } return YES; } - (void)loadAdWithPosid:(NSString *)posid customView:(UIView *)customView { if (_serviceAdView) { [_serviceAdView removeFromSuperview]; _serviceAdView = nil; } CGFloat height = customView.width; CGFloat width = customView.width; // CGFloat viewY = (customView.height-width); // 1 初始化banner视图 UIViewController *current_VC = RQControllerHelper.currentViewController; _serviceAdView = [[CXHAdSDKServiceAdView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; _serviceAdView.delegate = self; _serviceAdView.controller = current_VC; _serviceAdView.refershTime = 35; _serviceAdView.posId = posid; _serviceAdView.backgroundColor = [UIColor whiteColor]; // 2 添加到父视图上 _serviceAdView.frame = CGRectMake(0, 0, width, height); _serviceAdView.centerY = customView.centerY; [customView addSubview:_serviceAdView]; // 3 加载并显示广告 注意: 请确保banner视图显示在屏幕内的时候,调用load方法 [_serviceAdView loadAndShow]; } - (void)requestAgreePrivacy { [self writeAppLoad]; [self initCXHAdSDK]; } #pragma mark - PrivateMethods - (void)initCXHAdSDK { [CXHAdSDK setRequestDomain:@"http://ad.baihemob.com"]; [CXHAdSDK setLogLevel:CXHSDKLogLevelDebug]; NSString *CXHAppid = @"458110"; [CXHAdSDK initWithAppId:CXHAppid completionBlock:^(NSError * _Nullable error) { if (error) { NSLog(@"CXH初始化失败:%@", error); } }]; } - (void)initBaseData { [[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] map:^id(NSNotification *value) { return value.object; }] distinctUntilChanged] subscribeNext:^(id x) { NSLog(@"%@",@"APP进入后台"); }]; } #pragma mark - CXHSDKServiceAdViewDelegate /** 广告获取成功 @param serviceAdView banner实例 */ - (void)CXH_serviceAdViewDidReceived:(CXHAdSDKServiceAdView *)serviceAdView{ } /** 广告拉取失败 @param serviceAdView banner实例 @param error 错误描述 */ - (void)CXH_serviceAdViewFailToReceived:(CXHAdSDKServiceAdView *)serviceAdView error:(NSError *)error{ [_serviceAdView removeFromSuperview]; _serviceAdView = nil; } /** 广告点击 @param serviceAdView 广告实例 @param loadingPageURL 广告落地页地址,当渠道为bwt,并且customLoadingPage为YES时有值 */ - (void)CXH_serviceAdViewClicked:(CXHAdSDKServiceAdView *)serviceAdView loadingPageURL:(NSString *)loadingPageURL{ } /** 广告关闭 @param serviceAdView 广告实例 */ - (void)CXH_serviceAdViewClose:(CXHAdSDKServiceAdView *)serviceAdView{ dispatch_async(dispatch_get_main_queue(), ^{ _serviceAdView = nil; }); } /** 广告展示 @param serviceAdView 广告实例 */ - (void)CXH_serviceAdViewExposure:(CXHAdSDKServiceAdView *)serviceAdView{ } /** 关闭落地页 @param serviceAdView 广告实例 */ - (void)CXH_serviceAdViewCloseLandingPage:(CXHAdSDKServiceAdView *)serviceAdView{ } @end