123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- //
- // RQADViewManager.m
- // jiaPei
- //
- // Created by 张嵘 on 2021/4/29.
- // Copyright © 2021 JCZ. All rights reserved.
- //
- #import "RQADViewManager.h"
- #import <ADCDN/ADCDN.h>
- #if defined(__IPHONE_14_0)
- #import <AppTrackingTransparency/AppTrackingTransparency.h>//适配iOS14
- #endif
- static RQADViewManager *manger = nil;
- static dispatch_once_t onceToken;
- static NSString *appId = @"600446";
- static NSString *splashPlccId = @"2001569";
- static NSString *bannerPlccId = @"2001564";
- @interface RQADViewManager () <ADCDN_SplashAdManagerViewDelegate, ADCDN_BannerAdManagerDelegate>
- /* 开屏广告对象 */
- @property (nonatomic,strong) ADCDN_SplashAdManagerView *splashAdView;
- /* 横幅广告 */
- @property (nonatomic,strong) ADCDN_BannerAdManager *bannerAdManager;
- @property (nonatomic, readwrite, strong) UIView *customView;
- @property (nonatomic ,strong) dispatch_source_t myTimer;// 注意:此处应该使用强引用 strong
- @property (nonatomic, readwrite, assign) BOOL isResume;
- @property (nonatomic, readwrite, assign) RQADType adType;
- @property (nonatomic, readwrite, strong) RQADCloseCompletedBlock aDCloseCompletedBlock;
- @end
- @implementation RQADViewManager
- + (RQADViewManager *)sharedManager {
- dispatch_once(&onceToken, ^{
- manger = [[self alloc] init];
- });
- return manger;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- [self initBaseData];
- [self initADCDN];
- }
- return self;
- }
- - (void)initBaseData {
-
- }
- - (void)dealloc {
- [self stopTimer];
- }
- - (void)initCloseBlock:(RQADCloseCompletedBlock)block {
- self.aDCloseCompletedBlock = block;
- }
- //获取当前屏幕显示的viewcontroller
- - (UIViewController *)getCurrentVC {
- UIViewController *resultVC;
- resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
- return resultVC;
- }
- - (UIViewController *)_topViewController:(UIViewController *)vc {
- if ([vc isKindOfClass:[UINavigationController class]]) {
- return [self _topViewController:[(UINavigationController *)vc topViewController]];
- } else if ([vc isKindOfClass:[UITabBarController class]]) {
- return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
- } else {
- return vc;
- }
- }
- - (void)initADCDN {
- // 开发ADCDN错误日志,默认不开启
- [ADCDN_DebugLogTool setLogEnable:YES];
- if (@available(iOS 14, *)) {
- [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
- // 初始化配置
- [ADCDN_ConfigManager shareManagerWithAppId:appId];
- }];
- } else {
- // 初始化配置
- [ADCDN_ConfigManager shareManagerWithAppId:appId];
- }
- }
- - (void)initTimerWithSecound:(CGFloat)second {
- //2.设置时间等
- /*
- 第一个参数:定时器对象
- 第二个参数:DISPATCH_TIME_NOW 表示从现在开始计时
- 第三个参数:间隔时间 GCD里面的时间最小单位为 纳秒
- 第四个参数:精准度(表示允许的误差,0表示绝对精准)
- */
- dispatch_source_set_timer(self.myTimer, DISPATCH_TIME_NOW, second * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
- //3.要调用的任务
- dispatch_source_set_event_handler(self.myTimer, ^{
- NSLog(@"GCD-----%@",[NSThread currentThread]);
- [self.bannerAdManager loadNativeAd];
- if (![[self getCurrentVC] isKindOfClass:NSClassFromString(@"ExerciseVC")]) {
- NSLog(@"%s",object_getClassName([self getCurrentVC]));
- [self stopTimer];
- }
- });
- }
- - (void)pauseTimer {
- if (self.myTimer) {
- dispatch_suspend(_myTimer);
- }
- }
- - (void)resumeTimer {
- if (_myTimer) {
- dispatch_resume(_myTimer);
- self.isResume = YES;
- }
- }
-
- - (void)stopTimer {
- self.isResume = NO;
- if (_myTimer) {
- dispatch_source_cancel(_myTimer);
- _myTimer = nil;
- }
- }
- - (void)loadAdWithAdType:(RQADType)adType customView:(UIView *)customView {
- [self loadAdWithAdType:adType customView:customView cycleSecound:0.f];
- }
- - (void)loadAdWithAdType:(RQADType)adType customView:(UIView *)customView cycleSecound:(CGFloat)second {
- self.adType = adType;
- self.customView = customView;
- /// 服务端允许展示广告
- if (RQ_SHARE_FUNCTION.APP_AD) {
- switch (adType) {
- case RQADType_Splash: {
- /// 不展示广告在有引导页的时候
- if (!myDelegate.adIsShow) {
- [self.splashAdView loadSplashAd];
- }
- break;
- }
- case RQADType_Banner: {
- self.bannerAdManager.customView = customView;
- self.bannerAdManager.rootViewController = [self getCurrentVC];
- self.bannerAdManager.adSize = self.customView.size;
- if (second > 0) {
- if (!self.isResume) {
- [self initTimerWithSecound:second];
- [self resumeTimer];
- [self.bannerAdManager loadNativeAd];
- }
- } else {
- self.bannerAdManager.interval = 30;
- [self.bannerAdManager loadNativeAd];
- }
- break;
- }
- default:
- break;
- }
- }
- }
- #pragma mark - LazyLoad
- - (dispatch_source_t)myTimer {
- if (!_myTimer) {
- //0.创建队列
- dispatch_queue_t queue = dispatch_get_main_queue();
- //1.创建GCD中的定时器
- /*
- 第一个参数:创建source的类型 DISPATCH_SOURCE_TYPE_TIMER:定时器
- 第二个参数:0
- 第三个参数:0
- 第四个参数:队列
- */
- _myTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
- }
- return _myTimer;
- }
- - (ADCDN_SplashAdManagerView *)splashAdView{
- if (!_splashAdView) {
- CGRect frame = [UIScreen mainScreen].bounds;
- _splashAdView = [[ADCDN_SplashAdManagerView alloc] initWithFrame:frame plcId:splashPlccId];
- _splashAdView.backgroundColor = [UIColor whiteColor];
- _splashAdView.window = myDelegate.window;
- _splashAdView.delegate = self;// manager需要strong持有,否则delegate回调无法执行,影响计费
- [myDelegate.window.rootViewController.view addSubview:_splashAdView];
- _splashAdView.rootViewController = myDelegate.window.rootViewController;
- // 防止白屏,给一个兜底图,开发者可以设置一个跟启动图一样的
- UIView *bottomView = [[UIView alloc] initWithFrame:frame];
-
- UIViewController * vc = [[UIStoryboard storyboardWithName:@"Launch Screen" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
- CGRect rect = [UIScreen mainScreen].bounds;
- UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
- CGContextRef context = UIGraphicsGetCurrentContext();
- vc.view.frame = rect;
- [vc.view.layer renderInContext:context];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- UIImageView *bottomImgView = [[UIImageView alloc] initWithFrame:rect];
- bottomImgView.image = image;
- [bottomView addSubview:bottomImgView];
- _splashAdView.bottomView = bottomView;
-
-
-
- // // 设置开屏底部自定义LogoView,展示半屏开屏广告
- // UIView *logoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width * 0.25)];
- // UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:logoView.frame];
- // CGRect logoFrame = logoImageView.frame;
- // logoFrame.size.width = 123;
- // logoFrame.size.height = 30;
- // logoImageView.frame = logoFrame;
- // logoImageView.image = [UIImage imageNamed:@"splash_logo.png"];
- // [logoView addSubview:logoImageView];
- // logoImageView.center = logoView.center;
- // logoView.backgroundColor = [UIColor whiteColor];
- // _splashAdView.logoView = logoView;
- }
- return _splashAdView;
- }
- - (ADCDN_BannerAdManager *)bannerAdManager{
- if (!_bannerAdManager) {
- _bannerAdManager = [[ADCDN_BannerAdManager alloc] initWithPlcId:bannerPlccId];
- _bannerAdManager.customView = self.customView;// banner加载的位置
- _bannerAdManager.interval = 0;// 大于等于30s循环展示
- _bannerAdManager.rootViewController = [self getCurrentVC];
- _bannerAdManager.delegate = self;// banner需要strong持有,否则delegate回调无法执行,影响计费
- _bannerAdManager.adSize = self.customView.size;
- }
- return _bannerAdManager;
- }
- #pragma mark - ADCDN_SplashAdManagerViewDelegate
- /**
- * 开屏广告成功展示
- */
- - (void)ADCDN_SplashAdSuccessPresentScreen:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
- if (RQ_SHARE_FUNCTION.APP_AD && RQ_SHARE_FUNCTION.APP_CLOSE > 0) {
- for (UIView *view in splashAd.window.subviews) {
- if ([view isKindOfClass:[NSClassFromString(@"UITransitionView") class]]) {
- for (UIView *view0 in view.subviews) {
- if ([view0 isKindOfClass:[UIView class]]) {
- for (UIView *view1 in view0.subviews) {
- if ([view1 isKindOfClass:[NSClassFromString(@"GDTSplashSkipView") class]]) {
- UIButton *btn = (UIButton *)view1;
- NSInteger index = arc4random() % 100 + 1;
- [btn setEnabled:index <= RQ_SHARE_FUNCTION.APP_CLOSE];
- }
- }
- }
- }
- }
- }
- }
- NSLog(@"%s---%@",__FUNCTION__,@"开屏广告成功展示");
- }
- /**
- * 开屏广告展示失败
- */
- - (void)ADCDN_SplashAdFailToPresent:(ADCDN_SplashAdManagerView *_Nullable)splashAd withError:(NSError *_Nullable)error {
- NSLog(@"%s---%@ error:%@",__FUNCTION__,@"开屏广告展示失败",error);
- // 移除开屏视图
- if (self.splashAdView) {
- [self.splashAdView removeFromSuperview];
- self.splashAdView = nil;
- }
- }
- /**
- * 开屏广告曝光回调
- */
- - (void)ADCDN_SplashAdExposured:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
- NSLog(@"%s---%@",__FUNCTION__,@"开屏广告曝光回调");
- }
- /**
- * 开屏广告点击回调
- */
- - (void)ADCDN_SplashAdClicked:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
- NSLog(@"%s---%@",__FUNCTION__,@"开屏广告点击回调");
- }
- /**
- * 开屏广告关闭回调
- */
- - (void)ADCDN_SplashAdClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
- NSLog(@"%s---%@",__FUNCTION__,@"开屏广告关闭回调");
- // 移除开屏视图
- if (self.splashAdView) {
- [self.splashAdView removeFromSuperview];
- self.splashAdView = nil;
- }
- }
- /**
- * 开屏广告将要关闭回调
- */
- - (void)ADCDN_SplashAdWillClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd{
- NSLog(@"%s---%@",__FUNCTION__,@"开屏广告将要关闭回调");
-
- }
- /**
- * 开屏详情页关闭回调
- */
- - (void)ADCDN_SplashAdDetailClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd{
- NSLog(@"%s---%@",__FUNCTION__,@"开屏详情页关闭回调");
- }
- #pragma mark - ADCDN_BannerAdManagerDelegate代理
- /**
- * 加载成功
- */
- - (void)ADCDN_BannerAdDidLoad:(ADCDN_BannerAdManager *)bannerAd {
- NSLog(@"加载成功-----%s",__FUNCTION__);
- }
- /**
- * 加载失败
- */
- - (void)ADCDN_BannerAd:(ADCDN_BannerAdManager *)bannerAd didFailWithError:(NSError *_Nullable)error {
- NSLog(@"加载失败-----%s",__FUNCTION__);
- }
- /**
- * 点击广告
- */
- - (void)ADCDN_BannerAdDidClick:(ADCDN_BannerAdManager *)bannerAd {
- NSLog(@"点击广告时-----%s",__FUNCTION__);
- }
- /**
- * 曝光回调
- */
- - (void)ADCDN_BannerAdDidBecomeVisible:(ADCDN_BannerAdManager *)bannerAd {
- NSLog(@"曝光回调-----%s",__FUNCTION__);
- }
- /**
- * 关闭广告
- */
- - (void)ADCDN_BannerAdDidClose:(ADCDN_BannerAdManager *)bannerAd{
- NSLog(@"关闭回调-----%s",__FUNCTION__);
- [self stopTimer];
- if (self.aDCloseCompletedBlock) {
- self.aDCloseCompletedBlock(YES);
- }
- }
- @end
|