RQADViewManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //
  2. // RQADViewManager.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2021/4/29.
  6. // Copyright © 2021 JCZ. All rights reserved.
  7. //
  8. #import "RQADViewManager.h"
  9. #import <ADCDN/ADCDN.h>
  10. #if defined(__IPHONE_14_0)
  11. #import <AppTrackingTransparency/AppTrackingTransparency.h>//适配iOS14
  12. #endif
  13. static RQADViewManager *manger = nil;
  14. static dispatch_once_t onceToken;
  15. static NSString *appId = @"600446";
  16. static NSString *splashPlccId = @"2001569";
  17. static NSString *bannerPlccId = @"2001564";
  18. @interface RQADViewManager () <ADCDN_SplashAdManagerViewDelegate, ADCDN_BannerAdManagerDelegate>
  19. /* 开屏广告对象 */
  20. @property (nonatomic,strong) ADCDN_SplashAdManagerView *splashAdView;
  21. /* 横幅广告 */
  22. @property (nonatomic,strong) ADCDN_BannerAdManager *bannerAdManager;
  23. @property (nonatomic, readwrite, strong) UIView *customView;
  24. @property (nonatomic ,strong) dispatch_source_t myTimer;// 注意:此处应该使用强引用 strong
  25. @property (nonatomic, readwrite, assign) BOOL isResume;
  26. @property (nonatomic, readwrite, assign) RQADType adType;
  27. @property (nonatomic, readwrite, strong) RQADCloseCompletedBlock aDCloseCompletedBlock;
  28. @end
  29. @implementation RQADViewManager
  30. + (RQADViewManager *)sharedManager {
  31. dispatch_once(&onceToken, ^{
  32. manger = [[self alloc] init];
  33. });
  34. return manger;
  35. }
  36. - (instancetype)init {
  37. self = [super init];
  38. if (self) {
  39. [self initBaseData];
  40. [self initADCDN];
  41. }
  42. return self;
  43. }
  44. - (void)initBaseData {
  45. }
  46. - (void)dealloc {
  47. [self stopTimer];
  48. }
  49. - (void)initCloseBlock:(RQADCloseCompletedBlock)block {
  50. self.aDCloseCompletedBlock = block;
  51. }
  52. //获取当前屏幕显示的viewcontroller
  53. - (UIViewController *)getCurrentVC {
  54. UIViewController *resultVC;
  55. resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
  56. return resultVC;
  57. }
  58. - (UIViewController *)_topViewController:(UIViewController *)vc {
  59. if ([vc isKindOfClass:[UINavigationController class]]) {
  60. return [self _topViewController:[(UINavigationController *)vc topViewController]];
  61. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  62. return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
  63. } else {
  64. return vc;
  65. }
  66. }
  67. - (void)initADCDN {
  68. // 开发ADCDN错误日志,默认不开启
  69. [ADCDN_DebugLogTool setLogEnable:YES];
  70. if (@available(iOS 14, *)) {
  71. [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
  72. // 初始化配置
  73. [ADCDN_ConfigManager shareManagerWithAppId:appId];
  74. }];
  75. } else {
  76. // 初始化配置
  77. [ADCDN_ConfigManager shareManagerWithAppId:appId];
  78. }
  79. }
  80. - (void)initTimerWithSecound:(CGFloat)second {
  81. //2.设置时间等
  82. /*
  83. 第一个参数:定时器对象
  84. 第二个参数:DISPATCH_TIME_NOW 表示从现在开始计时
  85. 第三个参数:间隔时间 GCD里面的时间最小单位为 纳秒
  86. 第四个参数:精准度(表示允许的误差,0表示绝对精准)
  87. */
  88. dispatch_source_set_timer(self.myTimer, DISPATCH_TIME_NOW, second * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
  89. //3.要调用的任务
  90. dispatch_source_set_event_handler(self.myTimer, ^{
  91. NSLog(@"GCD-----%@",[NSThread currentThread]);
  92. [self.bannerAdManager loadNativeAd];
  93. if (![[self getCurrentVC] isKindOfClass:NSClassFromString(@"ExerciseVC")]) {
  94. NSLog(@"%s",object_getClassName([self getCurrentVC]));
  95. [self stopTimer];
  96. }
  97. });
  98. }
  99. - (void)pauseTimer {
  100. if (self.myTimer) {
  101. dispatch_suspend(_myTimer);
  102. }
  103. }
  104. - (void)resumeTimer {
  105. if (_myTimer) {
  106. dispatch_resume(_myTimer);
  107. self.isResume = YES;
  108. }
  109. }
  110. - (void)stopTimer {
  111. self.isResume = NO;
  112. if (_myTimer) {
  113. dispatch_source_cancel(_myTimer);
  114. _myTimer = nil;
  115. }
  116. }
  117. - (void)loadAdWithAdType:(RQADType)adType customView:(UIView *)customView {
  118. [self loadAdWithAdType:adType customView:customView cycleSecound:0.f];
  119. }
  120. - (void)loadAdWithAdType:(RQADType)adType customView:(UIView *)customView cycleSecound:(CGFloat)second {
  121. self.adType = adType;
  122. self.customView = customView;
  123. /// 服务端允许展示广告
  124. if (RQ_SHARE_FUNCTION.APP_AD) {
  125. switch (adType) {
  126. case RQADType_Splash: {
  127. /// 不展示广告在有引导页的时候
  128. if (!myDelegate.adIsShow) {
  129. [self.splashAdView loadSplashAd];
  130. }
  131. break;
  132. }
  133. case RQADType_Banner: {
  134. self.bannerAdManager.customView = customView;
  135. self.bannerAdManager.rootViewController = [self getCurrentVC];
  136. self.bannerAdManager.adSize = self.customView.size;
  137. if (second > 0) {
  138. if (!self.isResume) {
  139. [self initTimerWithSecound:second];
  140. [self resumeTimer];
  141. [self.bannerAdManager loadNativeAd];
  142. }
  143. } else {
  144. self.bannerAdManager.interval = 30;
  145. [self.bannerAdManager loadNativeAd];
  146. }
  147. break;
  148. }
  149. default:
  150. break;
  151. }
  152. }
  153. }
  154. #pragma mark - LazyLoad
  155. - (dispatch_source_t)myTimer {
  156. if (!_myTimer) {
  157. //0.创建队列
  158. dispatch_queue_t queue = dispatch_get_main_queue();
  159. //1.创建GCD中的定时器
  160. /*
  161. 第一个参数:创建source的类型 DISPATCH_SOURCE_TYPE_TIMER:定时器
  162. 第二个参数:0
  163. 第三个参数:0
  164. 第四个参数:队列
  165. */
  166. _myTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  167. }
  168. return _myTimer;
  169. }
  170. - (ADCDN_SplashAdManagerView *)splashAdView{
  171. if (!_splashAdView) {
  172. CGRect frame = [UIScreen mainScreen].bounds;
  173. _splashAdView = [[ADCDN_SplashAdManagerView alloc] initWithFrame:frame plcId:splashPlccId];
  174. _splashAdView.backgroundColor = [UIColor whiteColor];
  175. _splashAdView.window = myDelegate.window;
  176. _splashAdView.delegate = self;// manager需要strong持有,否则delegate回调无法执行,影响计费
  177. [myDelegate.window.rootViewController.view addSubview:_splashAdView];
  178. _splashAdView.rootViewController = myDelegate.window.rootViewController;
  179. // 防止白屏,给一个兜底图,开发者可以设置一个跟启动图一样的
  180. UIView *bottomView = [[UIView alloc] initWithFrame:frame];
  181. UIViewController * vc = [[UIStoryboard storyboardWithName:@"Launch Screen" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
  182. CGRect rect = [UIScreen mainScreen].bounds;
  183. UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
  184. CGContextRef context = UIGraphicsGetCurrentContext();
  185. vc.view.frame = rect;
  186. [vc.view.layer renderInContext:context];
  187. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  188. UIGraphicsEndImageContext();
  189. UIImageView *bottomImgView = [[UIImageView alloc] initWithFrame:rect];
  190. bottomImgView.image = image;
  191. [bottomView addSubview:bottomImgView];
  192. _splashAdView.bottomView = bottomView;
  193. // // 设置开屏底部自定义LogoView,展示半屏开屏广告
  194. // UIView *logoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width * 0.25)];
  195. // UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:logoView.frame];
  196. // CGRect logoFrame = logoImageView.frame;
  197. // logoFrame.size.width = 123;
  198. // logoFrame.size.height = 30;
  199. // logoImageView.frame = logoFrame;
  200. // logoImageView.image = [UIImage imageNamed:@"splash_logo.png"];
  201. // [logoView addSubview:logoImageView];
  202. // logoImageView.center = logoView.center;
  203. // logoView.backgroundColor = [UIColor whiteColor];
  204. // _splashAdView.logoView = logoView;
  205. }
  206. return _splashAdView;
  207. }
  208. - (ADCDN_BannerAdManager *)bannerAdManager{
  209. if (!_bannerAdManager) {
  210. _bannerAdManager = [[ADCDN_BannerAdManager alloc] initWithPlcId:bannerPlccId];
  211. _bannerAdManager.customView = self.customView;// banner加载的位置
  212. _bannerAdManager.interval = 0;// 大于等于30s循环展示
  213. _bannerAdManager.rootViewController = [self getCurrentVC];
  214. _bannerAdManager.delegate = self;// banner需要strong持有,否则delegate回调无法执行,影响计费
  215. _bannerAdManager.adSize = self.customView.size;
  216. }
  217. return _bannerAdManager;
  218. }
  219. #pragma mark - ADCDN_SplashAdManagerViewDelegate
  220. /**
  221. * 开屏广告成功展示
  222. */
  223. - (void)ADCDN_SplashAdSuccessPresentScreen:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
  224. if (RQ_SHARE_FUNCTION.APP_AD && RQ_SHARE_FUNCTION.APP_CLOSE > 0) {
  225. for (UIView *view in splashAd.window.subviews) {
  226. if ([view isKindOfClass:[NSClassFromString(@"UITransitionView") class]]) {
  227. for (UIView *view0 in view.subviews) {
  228. if ([view0 isKindOfClass:[UIView class]]) {
  229. for (UIView *view1 in view0.subviews) {
  230. if ([view1 isKindOfClass:[NSClassFromString(@"GDTSplashSkipView") class]]) {
  231. UIButton *btn = (UIButton *)view1;
  232. NSInteger index = arc4random() % 100 + 1;
  233. [btn setEnabled:index <= RQ_SHARE_FUNCTION.APP_CLOSE];
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. NSLog(@"%s---%@",__FUNCTION__,@"开屏广告成功展示");
  242. }
  243. /**
  244. * 开屏广告展示失败
  245. */
  246. - (void)ADCDN_SplashAdFailToPresent:(ADCDN_SplashAdManagerView *_Nullable)splashAd withError:(NSError *_Nullable)error {
  247. NSLog(@"%s---%@ error:%@",__FUNCTION__,@"开屏广告展示失败",error);
  248. // 移除开屏视图
  249. if (self.splashAdView) {
  250. [self.splashAdView removeFromSuperview];
  251. self.splashAdView = nil;
  252. }
  253. }
  254. /**
  255. * 开屏广告曝光回调
  256. */
  257. - (void)ADCDN_SplashAdExposured:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
  258. NSLog(@"%s---%@",__FUNCTION__,@"开屏广告曝光回调");
  259. }
  260. /**
  261. * 开屏广告点击回调
  262. */
  263. - (void)ADCDN_SplashAdClicked:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
  264. NSLog(@"%s---%@",__FUNCTION__,@"开屏广告点击回调");
  265. }
  266. /**
  267. * 开屏广告关闭回调
  268. */
  269. - (void)ADCDN_SplashAdClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd {
  270. NSLog(@"%s---%@",__FUNCTION__,@"开屏广告关闭回调");
  271. // 移除开屏视图
  272. if (self.splashAdView) {
  273. [self.splashAdView removeFromSuperview];
  274. self.splashAdView = nil;
  275. }
  276. }
  277. /**
  278. * 开屏广告将要关闭回调
  279. */
  280. - (void)ADCDN_SplashAdWillClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd{
  281. NSLog(@"%s---%@",__FUNCTION__,@"开屏广告将要关闭回调");
  282. }
  283. /**
  284. * 开屏详情页关闭回调
  285. */
  286. - (void)ADCDN_SplashAdDetailClosed:(ADCDN_SplashAdManagerView *_Nullable)splashAd{
  287. NSLog(@"%s---%@",__FUNCTION__,@"开屏详情页关闭回调");
  288. }
  289. #pragma mark - ADCDN_BannerAdManagerDelegate代理
  290. /**
  291. * 加载成功
  292. */
  293. - (void)ADCDN_BannerAdDidLoad:(ADCDN_BannerAdManager *)bannerAd {
  294. NSLog(@"加载成功-----%s",__FUNCTION__);
  295. }
  296. /**
  297. * 加载失败
  298. */
  299. - (void)ADCDN_BannerAd:(ADCDN_BannerAdManager *)bannerAd didFailWithError:(NSError *_Nullable)error {
  300. NSLog(@"加载失败-----%s",__FUNCTION__);
  301. }
  302. /**
  303. * 点击广告
  304. */
  305. - (void)ADCDN_BannerAdDidClick:(ADCDN_BannerAdManager *)bannerAd {
  306. NSLog(@"点击广告时-----%s",__FUNCTION__);
  307. }
  308. /**
  309. * 曝光回调
  310. */
  311. - (void)ADCDN_BannerAdDidBecomeVisible:(ADCDN_BannerAdManager *)bannerAd {
  312. NSLog(@"曝光回调-----%s",__FUNCTION__);
  313. }
  314. /**
  315. * 关闭广告
  316. */
  317. - (void)ADCDN_BannerAdDidClose:(ADCDN_BannerAdManager *)bannerAd{
  318. NSLog(@"关闭回调-----%s",__FUNCTION__);
  319. [self stopTimer];
  320. if (self.aDCloseCompletedBlock) {
  321. self.aDCloseCompletedBlock(YES);
  322. }
  323. }
  324. @end