RQADViewManager.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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 "YYTimer.h"
  10. // 支持广点通开屏广告v+
  11. #define SUPPORT_SPLASH_ZOOMOUT 1
  12. NSString * const RQADInterstitialNotificationName = @"RQADInterstitialNotificationName";
  13. #if defined(__IPHONE_14_0)
  14. #import <AppTrackingTransparency/AppTrackingTransparency.h>//适配iOS14
  15. #endif
  16. static RQADViewManager *manger = nil;
  17. static dispatch_once_t onceToken;
  18. static NSString *splashPlcId = @"cca7ca16b609a5f82f";
  19. static NSString *bannerPlcId = @"0702543ed7029244f5";
  20. static NSString *bannerPlcId2 = @"389c6b76a6fb66ea00";
  21. static NSString *homePageBannerPlcId = @"51fe9c4b24041952f6";
  22. static NSString *nativeAdPlcId = @"41c0bc7485d4afc80b";
  23. static NSString *intertitialAdPlcId = @"ee0c6fafec9f36389a";
  24. //插屏开屏广告
  25. static NSString *intertitialSplashAdPlcId = @"4077434dc98ce267ed";
  26. @interface RQADViewManager () <ADSuyiSDKBannerAdViewDelegate, ADSuyiSDKNativeAdDelegate,ADSuyiSDKIntertitialAdDelegate,
  27. //#ifdef SUPPORT_SPLASH_ZOOMOUT
  28. //ADSuyiSDKSplashAdZoomOutViewDelegate,
  29. //#endif
  30. ADSuyiSDKSplashAdDelegate
  31. >
  32. /* 插屏广告 */
  33. //@property (nonatomic,strong) ADCDN_InterstitialAdManager *interstitialAdManager;
  34. /* 开屏广告对象 */
  35. @property (nonatomic, readwrite, strong) ADSuyiSDKSplashAd *splashAd;
  36. /* 横幅广告 */
  37. @property (nonatomic, readwrite, strong) ADSuyiSDKBannerAdView *bannerView;
  38. /* 信息流广告 */
  39. @property (nonatomic, readwrite, strong) ADSuyiSDKNativeAd *nativeAd;
  40. /* 插屏广告 */
  41. @property (nonatomic, readwrite, strong) ADSuyiSDKIntertitialAd *intertitialAd;
  42. @property (nonatomic, readwrite, strong) UIView<ADSuyiAdapterNativeAdViewDelegate> *currentAdView;
  43. @property (nonatomic, readwrite, strong) UIView *customView;
  44. @property (nonatomic, readwrite, strong) UIViewController *customViewController;
  45. @property (nonatomic, readwrite, assign) BOOL isResume;
  46. @property (nonatomic, readwrite, copy) RQADCloseCompletedBlock aDCloseCompletedBlock;
  47. @property (nonatomic, readwrite, strong) UIButton *skipBtn;
  48. @property (nonatomic, readwrite, strong) YYTimer *timer;
  49. @property (nonatomic, readwrite, assign) NSUInteger count;
  50. @property (nonatomic, readwrite, assign) BOOL isReady;
  51. @property (nonatomic, readwrite, assign) BOOL isCanShow;
  52. @property (nonatomic, readwrite, copy) NSString *appId;
  53. @end
  54. @implementation RQADViewManager
  55. #pragma mark - SystemMethod
  56. + (RQADViewManager *)sharedManager {
  57. dispatch_once(&onceToken, ^{
  58. manger = [[self alloc] init];
  59. });
  60. return manger;
  61. }
  62. - (instancetype)init {
  63. self = [super init];
  64. if (self) {
  65. [self initBaseData];
  66. }
  67. return self;
  68. }
  69. - (void)dealloc {
  70. }
  71. #pragma mark - PublicMethods
  72. // 配置广告SDK
  73. - (void)rq_configureADSuiSDK {
  74. if ([RQ_AD_MANAGER isFirstAppLoad]) {
  75. [RQ_AD_MANAGER requestAgreePrivacy];
  76. } else {
  77. [RQ_AD_MANAGER initADSuyiSDK];
  78. }
  79. [NY_AD_MANAGER ny_configureADSuiSDK];
  80. }
  81. - (void)loadMineSplashAd {
  82. NSInteger randomAdNum = arc4random() % 100;
  83. RQADType splashAdType = (randomAdNum <= RQ_SHARE_FUNCTION.APP_KP)? RQADType_Splash : ([RQ_AD_MANAGER isFirstAppLoad]? RQADType_Splash : RQADType_InterstitialSplash);
  84. // if (splashAdType == RQADType_InterstitialSplash) {
  85. // RQ_APPDELEGATE.splashWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  86. // RQ_APPDELEGATE.splashWindow.backgroundColor = [UIColor whiteColor];
  87. // UIViewController * vc = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
  88. // [RQ_APPDELEGATE.splashWindow setRootViewController:vc];
  89. // [RQ_APPDELEGATE.splashWindow makeKeyAndVisible];
  90. // }
  91. // if (myDelegate.isLogin) {
  92. // [RQ_VIP_Module isVipWithSubject:0 complete:^(BOOL isVip) {
  93. // }];
  94. // if (RQ_COMMON_MANAGER.isHideSplashAD) {
  95. // ///会员,如果展示开屏需要把splashWindow隐藏
  96. // if (splashAdType == RQADType_InterstitialSplash) [self resetWindow];
  97. // } else {
  98. // [RQ_AD_MANAGER loadAdWithAdType:splashAdType customView:nil];/// 非会员,初始化广告SDK并展示开屏广告
  99. // }
  100. // } else {
  101. // [RQ_AD_MANAGER loadAdWithAdType:splashAdType customView:nil]; /// 无登录,初始化广告SDK并展示开屏广告
  102. // }
  103. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"APP_SWITCH"];
  104. if(object!=NULL){
  105. if(!RQ_SHARE_FUNCTION.APP_SWITCH){
  106. [RQ_AD_MANAGER loadAdWithAdType:splashAdType customView:nil];
  107. }
  108. }
  109. }
  110. - (void)loadAdWithAdType:(RQADType)adType customView:(UIView * _Nullable )customView {
  111. NSLog(@"VC==%@",customView);
  112. [self loadAdWithAdType:adType customView:customView cycleSecound:0.f];
  113. }
  114. - (void)loadAdWithAdType:(RQADType)adType customView:(UIView * _Nullable )customView cycleSecound:(CGFloat)second {
  115. [self loadAdWithAdType:adType customView:customView controller:RQControllerHelper.currentViewController cycleSecound:second];
  116. }
  117. - (void)loadAdWithAdType:(RQADType)adType customView:(UIView * _Nullable )customView controller:(UIViewController *)controller cycleSecound:(CGFloat)second {
  118. NSLog(@"===============VCC=%@",controller);
  119. @weakify(self)
  120. self.isNativeAdClicked = NO;
  121. self.adType = adType;
  122. self.customView = customView;
  123. self.customViewController = controller;
  124. if (adType == RQADType_Banner_IN) {
  125. self.bannerView.delegate = nil;
  126. self.bannerView = nil;
  127. [self.customView addSubview:self.bannerView];
  128. if(_splashAd==nil){
  129. [self.bannerView loadAndShow:self.customViewController];
  130. }
  131. if (second > 0) {
  132. RACSignal *deallocSignal = [self.customViewController rac_signalForSelector:@selector(viewDidDisappear:)];
  133. [[[RACSignal interval:second onScheduler:[RACScheduler mainThreadScheduler]] takeUntil: deallocSignal] subscribeNext:^(NSDate * _Nullable x) {
  134. @strongify(self)
  135. if (self.customView.hidden) {
  136. [self.customView addSubview:self.bannerView];
  137. if(_splashAd==nil){
  138. [self.bannerView loadAndShow:self.customViewController];
  139. }
  140. }
  141. }];
  142. }
  143. } else {
  144. /// 服务端允许展示广告
  145. if (RQ_COMMON_MANAGER.JSJP_APP_AD_OPEN) {
  146. if (RQ_VIP_Module.isVip) {
  147. switch (adType) {
  148. case RQADType_Splash: {
  149. [self loadSplashAd];
  150. break;
  151. }
  152. case RQADType_InterstitialSplash: {
  153. /// 非两个广告以上
  154. if (!RQ_VIP_Module.isADVip) {
  155. int a = arc4random() % 100;
  156. NSLog(@"RATE=%zd AD=%d",RQ_COMMON_MANAGER.JSJP_APP_AD_RATE,a);
  157. int date = [RQ_USER_MANAGER.city_dbId isEqualToString:@"3502"]?RQ_COMMON_MANAGER.JSJP_APP_XM_AD_RATE:RQ_COMMON_MANAGER.JSJP_APP_AD_RATE;
  158. if (date > a) {
  159. self.isReady = NO;
  160. _intertitialAd = nil;
  161. NSLog(@"AD===VC=%@",self.intertitialAd.controller);
  162. [self.intertitialAd loadAdData];
  163. }
  164. }
  165. break;
  166. }
  167. case RQADType_Banner: {
  168. if (RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne) {
  169. } else if (RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour) {
  170. } else {
  171. self.bannerView.delegate = nil;
  172. self.bannerView = nil;
  173. [self.customView addSubview:self.bannerView];
  174. if(_splashAd==nil){
  175. [self.bannerView loadAndShow:self.customViewController];
  176. }
  177. if (second > 0) {
  178. RACSignal *deallocSignal = [self.customViewController rac_signalForSelector:@selector(viewDidDisappear:)];
  179. [[[RACSignal interval:second onScheduler:[RACScheduler mainThreadScheduler]] takeUntil: deallocSignal] subscribeNext:^(NSDate * _Nullable x) {
  180. @strongify(self)
  181. if (self.customView.hidden) {
  182. [self.customView addSubview:self.bannerView];
  183. if(_splashAd==nil){
  184. [self.bannerView loadAndShow:self.customViewController];
  185. }
  186. }
  187. }];
  188. }
  189. }
  190. break;
  191. }
  192. default:
  193. break;
  194. }
  195. } else {
  196. switch (adType) {
  197. case RQADType_Splash: {
  198. [self loadSplashAd];
  199. break;
  200. }
  201. case RQADType_InterstitialSplash: {
  202. int a = arc4random() % 100;
  203. NSLog(@"RATE=%zd AD=%d",RQ_COMMON_MANAGER.JSJP_APP_AD_RATE,a);
  204. int date = [RQ_USER_MANAGER.city_dbId isEqualToString:@"3502"]?RQ_COMMON_MANAGER.JSJP_APP_XM_AD_RATE:RQ_COMMON_MANAGER.JSJP_APP_AD_RATE;
  205. if (date > a) {
  206. self.isReady = NO;
  207. _intertitialAd = nil;
  208. NSLog(@"AD===VC=%@",self.intertitialAd.controller);
  209. [self.intertitialAd loadAdData];
  210. }
  211. break;
  212. }
  213. case RQADType_Banner: {
  214. self.bannerView.delegate = nil;
  215. self.bannerView = nil;
  216. [self.customView addSubview:self.bannerView];
  217. if(_splashAd==nil){
  218. [self.bannerView loadAndShow:self.customViewController];
  219. }
  220. if (second > 0) {
  221. RACSignal *deallocSignal = [self.customViewController rac_signalForSelector:@selector(viewDidDisappear:)];
  222. [[[RACSignal interval:second onScheduler:[RACScheduler mainThreadScheduler]] takeUntil: deallocSignal] subscribeNext:^(NSDate * _Nullable x) {
  223. @strongify(self)
  224. if (self.customView.hidden) {
  225. [self.customView addSubview:self.bannerView];
  226. if(_splashAd==nil){
  227. [self.bannerView loadAndShow:self.customViewController];
  228. }
  229. }
  230. }];
  231. }
  232. break;
  233. }
  234. case RQADType_native: {
  235. [self.nativeAd load:1];
  236. }
  237. break;
  238. case RQADType_fullScreen: {
  239. self.isReady = NO;
  240. _intertitialAd = nil;
  241. NSLog(@"AD===VC=%@",self.intertitialAd.controller);
  242. [_intertitialAd loadAdData];
  243. }
  244. break;
  245. default:
  246. break;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. - (void)showAdWithAdType:(RQADType)adType {
  253. if (RQ_COMMON_MANAGER.JSJP_APP_AD_OPEN) {
  254. switch (adType) {
  255. case RQADType_fullScreen: {
  256. if (self.isReady) {
  257. self.isCanShow = YES;
  258. [self.intertitialAd show];
  259. }
  260. }
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. }
  267. - (void)initCloseBlock:(RQADCloseCompletedBlock)block {
  268. self.aDCloseCompletedBlock = block;
  269. }
  270. - (BOOL)isFirstAppLoad {
  271. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  272. if ([[userDefault objectForKey:@"isFirstLoad"] isEqualToString:@"yes"]) {
  273. return NO;
  274. }
  275. return YES;
  276. }
  277. #pragma mark - PrivateMethods
  278. - (void)initBaseData {
  279. [[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] map:^id(NSNotification *value) {
  280. return value.object;
  281. }] distinctUntilChanged] subscribeNext:^(id x) {
  282. NSLog(@"%@",@"APP进入后台");
  283. }];
  284. }
  285. - (void)requestAgreePrivacy {
  286. UIApplication *applicaiton = [UIApplication sharedApplication];
  287. if (applicaiton.applicationState == UIApplicationStateActive) {
  288. if (@available(iOS 14, *)) {
  289. ATTrackingManagerAuthorizationStatus status = [ATTrackingManager trackingAuthorizationStatus];
  290. if(status == ATTrackingManagerAuthorizationStatusNotDetermined||
  291. status == ATTrackingManagerAuthorizationStatusDenied) {
  292. [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
  293. // Tracking authorization completed. Start loading ads here.
  294. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  295. [self writeAppLoad];
  296. [self initADSuyiSDK];
  297. [NY_AD_MANAGER requestAgreePrivacy];
  298. }];
  299. }];
  300. } else {
  301. [self writeAppLoad];
  302. [self initADSuyiSDK];
  303. [NY_AD_MANAGER requestAgreePrivacy];
  304. }
  305. } else {
  306. [self writeAppLoad];
  307. [self initADSuyiSDK];
  308. [NY_AD_MANAGER requestAgreePrivacy];
  309. }
  310. } else {
  311. [self performSelector:@selector(requestAgreePrivacy)
  312. withObject:nil
  313. afterDelay:0.5f];
  314. }
  315. }
  316. - (void)writeAppLoad {
  317. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  318. [userDefault setObject:@"yes" forKey:@"isFirstLoad"];
  319. [userDefault synchronize];
  320. }
  321. - (void)initADSuyiSDK {
  322. // 设置日志输出等级
  323. [ADSuyiSDK setLogLevel:ADSuyiKitLogLevelDebug];
  324. // [ADSuyiSDK setOnlyPlatform:@"gdt"];
  325. // ADSuyiSDK初始化
  326. [ADSuyiSDK initWithAppId:self.appId completionBlock:^(NSError * _Nonnull error) {
  327. if (error) {
  328. NSLog(@"SDK 初始化失败:%@", error.localizedDescription);
  329. }
  330. }];
  331. }
  332. //- (void)resetWindow {
  333. // RQ_APPDELEGATE.splashWindow.hidden = YES;
  334. // [RQ_APPDELEGATE.splashWindow resignKeyWindow];
  335. // RQ_APPDELEGATE.splashWindow = nil;
  336. // [RQ_APPDELEGATE.window makeKeyAndVisible];
  337. //}
  338. - (void)_timerValueChanged:(YYTimer *)timer {
  339. self.count--;
  340. if (self.count == 0) {
  341. [timer invalidate];
  342. self.timer = nil;
  343. [self.skipBtn setTitleNormal:@"跳过"];
  344. if (self.currentAdView) {
  345. [self.currentAdView adsy_close];
  346. }
  347. return;
  348. }
  349. [self.skipBtn setTitleNormal:[NSString stringWithFormat:@"跳过 %zd", self.count]];
  350. }
  351. - (void)loadSplashAd {
  352. if (self.splashAd) {
  353. return;
  354. }
  355. self.splashAd = [[ADSuyiSDKSplashAd alloc]init];
  356. self.splashAd.delegate = self;
  357. self.splashAd.controller = RQControllerHelper.currentViewController;
  358. self.splashAd.posId = splashPlcId;
  359. self.splashAd.tolerateTimeout = 4;
  360. UIViewController * vc = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
  361. CGRect rect = [UIScreen mainScreen].bounds;
  362. UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
  363. CGContextRef context = UIGraphicsGetCurrentContext();
  364. vc.view.frame = rect;
  365. [vc.view.layer renderInContext:context];
  366. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  367. UIGraphicsEndImageContext();
  368. self.splashAd.backgroundColor = [UIColor adsy_getColorWithImage:image withNewSize:[UIScreen mainScreen].bounds.size];
  369. [self.splashAd loadAndShowInWindow:RQ_APPDELEGATE.window];
  370. }
  371. //获取当前屏幕显示的viewcontroller
  372. - (UIViewController *)getCurrentVC {
  373. UIViewController *resultVC;
  374. resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
  375. /// RQ Fixed : 这里必须要判断一下,否则取出来永远都是 RQMainTabBarViewController。这是架构上小缺(特)陷(性)。因为RQMainTabBarViewController的子控制器是UITabBarController,所以需要递归UITabBarController的所有的子控制器
  376. if ([resultVC isKindOfClass:[RQTableViewController class]]) {
  377. RQTableViewController *mainVc = (RQTableViewController *)resultVC;
  378. resultVC = [self _topViewController:mainVc.tabBarController];
  379. }
  380. return resultVC;
  381. }
  382. - (UIViewController *)_topViewController:(UIViewController *)vc {
  383. if ([vc isKindOfClass:[UINavigationController class]]) {
  384. return [self _topViewController:[(UINavigationController *)vc topViewController]];
  385. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  386. return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
  387. } else {
  388. return vc;
  389. }
  390. }
  391. #pragma mark - ADSuyiSDKSplashAdDelegate
  392. /**
  393. 开屏展现成功
  394. @param splashAd 广告实例
  395. */
  396. - (void)adsy_splashAdSuccessToPresentScreen:(ADSuyiSDKSplashAd *)splashAd{
  397. // [self resetWindow];
  398. }
  399. /**
  400. 开屏展现失败
  401. @param splashAd 广告实例
  402. @param error 具体错误信息
  403. */
  404. - (void)adsy_splashAdFailToPresentScreen:(ADSuyiSDKSplashAd *)splashAd failToPresentScreen:(ADSuyiAdapterErrorDefine *)error{
  405. _splashAd = nil;
  406. // [self resetWindow];
  407. }
  408. /**
  409. 开屏广告点击
  410. @param splashAd 广告实例
  411. */
  412. - (void)adsy_splashAdClicked:(ADSuyiSDKSplashAd *)splashAd{
  413. }
  414. /**
  415. 开屏被关闭
  416. @param splashAd 广告实例
  417. */
  418. - (void)adsy_splashAdClosed:(ADSuyiSDKSplashAd *)splashAd{
  419. _splashAd = nil;
  420. if (self.isReady && _intertitialAd) {
  421. UIViewController *vc = RQControllerHelper.currentViewController;
  422. _intertitialAd.controller = vc.tabBarController;
  423. [_intertitialAd show];
  424. }
  425. if (_bannerView.superview) {
  426. [_bannerView loadAndShow:self.customViewController];
  427. }
  428. // [RQNotificationCenter postNotificationName:RQADInterstitialNotificationName object:nil];
  429. }
  430. /**
  431. 开屏展示
  432. @param splashAd 广告实例
  433. */
  434. - (void)adsy_splashAdEffective:(ADSuyiSDKSplashAd *)splashAd{
  435. }
  436. #pragma mark - ADSuyiSDKBannerAdViewDelegate
  437. /**
  438. 广告获取成功
  439. @param bannerView banner实例
  440. */
  441. - (void)adsy_bannerViewDidReceived:(ADSuyiSDKBannerAdView *)bannerView {
  442. NSLog(@"bannerView--获取成功");
  443. // if (self.adType == RQADType_Banner_IN) {
  444. // bannerView.qmui_width = RQ_SCREEN_WIDTH - 32.f;
  445. // }
  446. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Success) : nil;
  447. }
  448. /**
  449. 广告拉取失败
  450. @param bannerView banner实例
  451. @param errorModel 错误描述
  452. */
  453. - (void)adsy_bannerViewFailToReceived:(ADSuyiSDKBannerAdView *)bannerView errorModel:(ADSuyiAdapterErrorDefine *)errorModel{
  454. NSLog(@"adsy_bannerViewFailToReceived:%@, %@",errorModel.errorDescription, errorModel.errorDetailDict);
  455. // [_bannerView removeFromSuperview];
  456. // _bannerView = nil;
  457. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Faild) : nil;
  458. }
  459. /**
  460. 广告点击
  461. @param bannerView 广告实例
  462. */
  463. - (void)adsy_bannerViewClicked:(ADSuyiSDKBannerAdView *)bannerView{
  464. }
  465. /**
  466. 广告关闭
  467. @param bannerView 广告实例
  468. */
  469. - (void)adsy_bannerViewClose:(ADSuyiSDKBannerAdView *)bannerView{
  470. // [_bannerView removeFromSuperview];
  471. // _bannerView = nil;
  472. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Close) : nil;
  473. }
  474. /**
  475. 广告展示
  476. @param bannerView 广告实例
  477. */
  478. - (void)adsy_bannerViewExposure:(ADSuyiSDKBannerAdView *)bannerView{
  479. }
  480. - (void)adsy_bannerAdCloseLandingPage:(nonnull ADSuyiSDKBannerAdView *)bannerView {
  481. }
  482. - (void)adsy_bannerViewDidPresent:(nonnull ADSuyiSDKBannerAdView *)bannerView {
  483. }
  484. #pragma mark - ADSuyiSDKNativeAdDelegate
  485. - (void)adsy_nativeAdSucessToLoad:(ADSuyiSDKNativeAd *)nativeAd
  486. adViewArray:(NSArray<__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *> *)adViewArray {
  487. for (UIView<ADSuyiAdapterNativeAdViewDelegate> *adView in adViewArray) {
  488. // 4、判断信息流广告是否为自渲染类型(可选实现) 可仿照所示样式demo实现 如无所需样式则需自行实现
  489. // 如果单纯只配置了模版信息流,那么不需要实现,如果配置了自渲染信息流,那么需要实现
  490. if(adView.renderType == ADSuyiAdapterRenderTypeNative) {
  491. // 4.1、如果是自渲染类型则可样式自定义(3种示例demo样式见下)
  492. // 1、常规样式
  493. // [self setUpUnifiedTopImageNativeAdView:adView];
  494. // 2、纯图样式
  495. [self setUpUnifiedOnlyImageNativeAdView:adView];
  496. // 3、上图下文
  497. // [self setUpUnifiedTopImageNativeAdView:adView];
  498. }
  499. // 5、注册,自渲染:注册点击事件,模板:render,重要
  500. [adView adsy_registViews:@[adView]];
  501. // 广点通视频信息流广告会给mediaView添加事件,点击会出现半屏广告,以下为广点通官方给予的解决方案
  502. if([adView.adsy_platform isEqualToString:ADSuyiAdapterPlatformGDT]
  503. && adView.renderType == ADSuyiAdapterRenderTypeNative
  504. && adView.data.shouldShowMediaView) {
  505. UIView *mediaView = [adView adsy_mediaViewForWidth:0.0];
  506. mediaView.userInteractionEnabled = NO;
  507. }
  508. }
  509. }
  510. - (void)adsy_nativeAdFailToLoad:(ADSuyiSDKNativeAd *)nativeAd
  511. errorModel:(ADSuyiAdapterErrorDefine *)errorModel {
  512. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Faild) : nil;
  513. }
  514. - (void)adsy_nativeAdViewRenderOrRegistSuccess:(UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  515. self.currentAdView = adView;
  516. for (UIView *subview in self.customView.subviews) {
  517. if ([subview.className isEqualToString:@"ADSuyiGDTUnifiedNativeAdView"]) {
  518. [subview removeFromSuperview];
  519. }
  520. }
  521. [self.customView addSubview:self.currentAdView];
  522. }
  523. - (void)adsy_nativeAdViewRenderOrRegistFail:(UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  524. self.currentAdView = adView;
  525. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Faild) : nil;
  526. }
  527. - (void)adsy_nativeAdClicked:(ADSuyiSDKNativeAd *)nativeAd
  528. adView:(__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  529. self.isNativeAdClicked = YES;
  530. if(RQ_AD_MANAGER.adType==RQADType_fullScreen||RQ_AD_MANAGER.adType==RQADType_native){
  531. return;
  532. }
  533. self.currentAdView = adView;
  534. [self.timer invalidate];
  535. self.timer = nil;
  536. [adView adsy_close];
  537. }
  538. - (void)adsy_nativeAdClose:(ADSuyiSDKNativeAd *)nativeAd
  539. adView:(__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  540. if(!self.isNativeAdClicked) {
  541. if (self.currentAdView) {
  542. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Close) : nil;
  543. self.currentAdView = adView;
  544. [self.currentAdView removeFromSuperview];
  545. [self.skipBtn removeFromSuperview];
  546. [UIView removeSubviewsRecursively:self.currentAdView];
  547. self.currentAdView = nil;
  548. self.nativeAd = nil;
  549. }
  550. }
  551. }
  552. - (void)adsy_nativeAdExposure:(ADSuyiSDKNativeAd *)nativeAd
  553. adView:(__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  554. self.currentAdView = adView;
  555. [self.customView bringSubviewToFront:self.skipBtn];
  556. self.count = 5;
  557. [_skipBtn setTitleNormal:@"跳过 5"];
  558. self.timer = [YYTimer timerWithTimeInterval:1 target:self selector:@selector(_timerValueChanged:) repeats:YES];
  559. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Success) : nil;
  560. }
  561. - (void)adsy_nativeAdCloseLandingPage:(ADSuyiSDKNativeAd *)nativeAd adView:(__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView{
  562. if(self.isNativeAdClicked) {
  563. if (self.currentAdView) {
  564. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Close) : nil;
  565. self.currentAdView = adView;
  566. [self.currentAdView removeFromSuperview];
  567. [self.skipBtn removeFromSuperview];
  568. [UIView removeSubviewsRecursively:self.currentAdView];
  569. self.currentAdView = nil;
  570. self.nativeAd = nil;
  571. }
  572. NSLog(@"adsy_nativeAdCloseLandingPage");
  573. }
  574. }
  575. - (void)adsy_nativeAd:(ADSuyiSDKNativeAd *)nativeAd
  576. adView:(nonnull __kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView playerStatusChanged:(ADSuyiAdapterNativeMediaPlayerStatus)status{
  577. NSLog(@"status = %zd",status);
  578. }
  579. #pragma mark - Helper 自渲染类型信息流处理方法 setUpUnifiedOnlyImageNativeAdView纯图样式
  580. // 纯图样式
  581. - (void)setUpUnifiedOnlyImageNativeAdView:(UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  582. // 设计的adView实际大小,其中宽度和高度可以自己根据自己的需求设置
  583. CGFloat adWidth = self.customView.frame.size.width;
  584. CGFloat adHeight = adWidth / 16.0 * 9;
  585. adView.frame = CGRectMake(0, 0, adWidth, adHeight);
  586. // 设置主图/视频(主图可选,但强烈建议带上,如果有视频试图,则必须带上)
  587. CGRect mainFrame = CGRectMake(0, 0, adWidth, adHeight);
  588. if(adView.data.shouldShowMediaView) {
  589. UIView *mediaView = [adView adsy_mediaViewForWidth:mainFrame.size.width];
  590. mediaView.frame = mainFrame;
  591. [adView addSubview:mediaView];
  592. } else {
  593. UIImageView *imageView = [UIImageView new];
  594. imageView.backgroundColor = [UIColor adsy_colorWithHexString:@"#CCCCCC"];
  595. [adView addSubview:imageView];
  596. imageView.frame = mainFrame;
  597. NSString *urlStr = adView.data.imageUrl;
  598. if(urlStr.length > 0) {
  599. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
  600. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]];
  601. dispatch_async(dispatch_get_main_queue(), ^{
  602. imageView.image = image;
  603. });
  604. });
  605. }
  606. }
  607. // 展示关闭按钮(必要)
  608. [self.customView addSubview:self.skipBtn];
  609. self.skipBtn.frame = CGRectMake(adWidth - 12 - 54, 12, 54, 27);
  610. [self.skipBtn addTarget:self action:@selector(clickClose) forControlEvents:UIControlEventTouchUpInside];
  611. // 显示logo图片(必要)
  612. if(![adView.adsy_platform isEqualToString:ADSuyiAdapterPlatformGDT]) { // 优量汇(广点通)会自带logo,不需要添加
  613. UIImageView *logoImage = [UIImageView new];
  614. [adView addSubview:logoImage];
  615. // [adView bringSubviewToFront:logoImage];
  616. [adView adsy_platformLogoImageDarkMode:NO loadImageBlock:^(UIImage * _Nullable image) {
  617. CGFloat maxWidth = 40;
  618. CGFloat logoHeight = maxWidth / image.size.width * image.size.height;
  619. logoImage.frame = CGRectMake(adWidth - maxWidth, adHeight - logoHeight, maxWidth, logoHeight);
  620. logoImage.image = image;
  621. }];
  622. }
  623. }
  624. - (void)clickClose {
  625. [self.timer invalidate];
  626. self.timer = nil;
  627. if (self.currentAdView) {
  628. [self.currentAdView adsy_close];
  629. }
  630. }
  631. #pragma mark - ADSuyiSDKIntertitialAdDelegate
  632. /**
  633. ADSuyiSDKIntertitialAd请求成功回调
  634. @param interstitialAd 插屏广告实例对象
  635. */
  636. - (void)adsy_interstitialAdSuccedToLoad:(ADSuyiSDKIntertitialAd *)interstitialAd {
  637. self.isReady = YES;
  638. if (!RQ_Video_Module.playerController.isPlaying && self.adType == RQADType_InterstitialSplash && _intertitialAd) {
  639. [_intertitialAd show];
  640. }
  641. }
  642. /**
  643. ADSuyiSDKIntertitialAd请求失败回调
  644. @param interstitialAd 插屏广告实例对象
  645. @param error 失败原因
  646. */
  647. - (void)adsy_interstitialAdFailedToLoad:(ADSuyiSDKIntertitialAd *)interstitialAd error:(ADSuyiAdapterErrorDefine *)error {
  648. // dispatch_async(dispatch_get_main_queue(), ^{
  649. // if (self.adType == RQADType_InterstitialSplash) {
  650. // [self resetWindow];
  651. // [RQ_AD_MANAGER loadAdWithAdType:RQADType_Splash customView:nil];
  652. // }
  653. // _intertitialAd = nil;
  654. // });
  655. }
  656. /**
  657. ADSuyiSDKIntertitialAd展示在屏幕内回调
  658. @param interstitialAd 插屏广告实例对象
  659. */
  660. - (void)adsy_interstitialAdDidPresent:(ADSuyiSDKIntertitialAd *)interstitialAd {
  661. }
  662. /**
  663. ADSuyiSDKIntertitialAd展示在屏幕内失败回调
  664. @param interstitialAd 插屏广告实例对象
  665. */
  666. - (void)adsy_interstitialAdFailedToPresent:(ADSuyiSDKIntertitialAd *)interstitialAd error:(NSError *)error {
  667. // dispatch_async(dispatch_get_main_queue(), ^{
  668. // if (self.adType == RQADType_InterstitialSplash) {
  669. // [self resetWindow];
  670. // [RQ_AD_MANAGER loadAdWithAdType:RQADType_Splash customView:nil];
  671. // }
  672. // });
  673. }
  674. /**
  675. ADSuyiSDKIntertitialAd点击回调
  676. @param interstitialAd 插屏广告实例对象
  677. */
  678. - (void)adsy_interstitialAdDidClick:(ADSuyiSDKIntertitialAd *)interstitialAd {
  679. }
  680. /**
  681. ADSuyiSDKIntertitialAd关闭回调
  682. @param interstitialAd 插屏广告实例对象
  683. */
  684. - (void)adsy_interstitialAdDidClose:(ADSuyiSDKIntertitialAd *)interstitialAd {
  685. // dispatch_async(dispatch_get_main_queue(), ^{
  686. // if (self.adType == RQADType_InterstitialSplash) {
  687. // [self resetWindow];
  688. // }
  689. // });
  690. }
  691. /**
  692. ADSuyiSDKIntertitialAd展示回调
  693. @param interstitialAd 广告实例
  694. */
  695. - (void)adsy_interstitialAdExposure:(ADSuyiSDKIntertitialAd *)interstitialAd {
  696. // if (RQ_SHARE_FUNCTION.APP_CLOSE > 0) {
  697. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(RQ_SHARE_FUNCTION.APP_CLOSE * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  698. // if (self.adType == RQADType_InterstitialSplash) {
  699. // [self resetWindow];
  700. // }
  701. // });
  702. // }
  703. }
  704. - (void)adsy_interstitialAdCloseLandingPage:(nonnull ADSuyiSDKIntertitialAd *)interstitialAd {
  705. }
  706. #pragma mark - LazyLoad
  707. - (UIView *)customView {
  708. if (!_customView) {
  709. CGRect rect;
  710. switch (self.adType) {
  711. case RQADType_Splash:
  712. rect = CGRectMake(0, 0, RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT * 0.75);
  713. break;
  714. case RQADType_Banner:
  715. rect = CGRectMake(25, (RQ_SCREEN_HEIGHT - (RQ_APPLICATION_NAV_BAR_HEIGHT + RQ_APPLICATION_STATUS_BAR_HEIGHT) - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT) - 60 - 40, RQ_SCREEN_WIDTH - 50, 50);
  716. case RQADType_Banner_IN:
  717. rect = CGRectMake(0, 0, RQ_SCREEN_WIDTH - 32.f, (RQ_SCREEN_WIDTH - 32.f) / (600 / 150.f));
  718. default:
  719. rect = CGRectMake(0, 0, RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT);
  720. break;
  721. }
  722. _customView = [[UIView alloc] initWithFrame:rect];
  723. }
  724. return _customView;
  725. }
  726. - (ADSuyiSDKBannerAdView *)bannerView {
  727. if (!_bannerView) {
  728. // 1、初始化banner视图,并给定frame值,rate取值根据banner的尺寸
  729. _bannerView = [[ADSuyiSDKBannerAdView alloc] initWithFrame:CGRectMake(0, 0, self.customView.width, self.customView.height)];
  730. _bannerView.delegate = self;
  731. // 2、设置控制器,用来弹出落地页,重要
  732. // _bannerView.controller = self.customViewController;
  733. // 3、设置广告位id,重要
  734. if (self.adType == RQADType_Banner_IN) {
  735. _bannerView.posId = homePageBannerPlcId;
  736. CGFloat height = self.customView.width / (600 / 150.f) - (iPhone6?3.f:4.f);
  737. if (height > self.customView.height) {
  738. _bannerView.frame = CGRectMake(0, - (height - self.customView.height) / 2.f, self.customView.width, height);
  739. } else {
  740. _bannerView.frame = CGRectMake(0, (height - self.customView.height) / 2.f, self.customView.width, height);
  741. }
  742. _bannerView.backgroundColor = UIColor.clearColor;
  743. } else {
  744. CGFloat a = RQ_SCREEN_WIDTH / (640/100.0);
  745. if (self.customView.height > a) {
  746. _bannerView.posId = bannerPlcId2;
  747. } else {
  748. /// (640 / 100.f)
  749. _bannerView.posId = bannerPlcId;
  750. }
  751. // 可设置bannerview的背景色,请按需添加修改该行代码
  752. _bannerView.backgroundColor = UIColor.whiteColor;
  753. }
  754. }
  755. return _bannerView;
  756. }
  757. - (ADSuyiSDKNativeAd *)nativeAd {
  758. if (!_nativeAd) {
  759. _nativeAd = [[ADSuyiSDKNativeAd alloc] initWithAdSize:self.customView.size];
  760. _nativeAd.delegate = self;
  761. _nativeAd.controller = RQControllerHelper.currentViewController;
  762. _nativeAd.posId = nativeAdPlcId;
  763. }
  764. return _nativeAd;
  765. }
  766. - (ADSuyiSDKIntertitialAd *)intertitialAd {
  767. if (!_intertitialAd) {
  768. _intertitialAd = [ADSuyiSDKIntertitialAd new];
  769. UIViewController *vc = RQControllerHelper.currentViewController.tabBarController;
  770. _intertitialAd.controller = vc;
  771. _intertitialAd.posId = (_adType == RQADType_fullScreen)? intertitialAdPlcId : intertitialSplashAdPlcId;
  772. _intertitialAd.delegate = self;
  773. _intertitialAd.tolerateTimeout = 4;
  774. // _intertitialAd.adSize = (_adType == RQADType_InterstitialSplash)? CGSizeMake(vc.view.frame.size.width / 2.f, vc.view.frame.size.height / 2.f) : CGSizeMake(vc.view.frame.size.width, vc.view.frame.size.height);
  775. }
  776. return _intertitialAd;
  777. }
  778. - (UIButton *)skipBtn {
  779. if (!_skipBtn) {
  780. _skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  781. _skipBtn.frame = CGRectMake(RQ_SCREEN_WIDTH - 54 - 12, 12, 54, 27);
  782. [_skipBtn setBackgroundColor:[UIColor colorWithWhite:0.1 alpha:0.3]];
  783. // [_skipBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.1 alpha:1] size:CGSizeMake(200, 200)] forState:UIControlStateNormal];
  784. _skipBtn.titleLabel.font = RQRegularFont(13);
  785. [_skipBtn setTitleNormal:@"跳过 5"];
  786. _skipBtn.layer.cornerRadius = 13.5;
  787. _skipBtn.clipsToBounds = YES;
  788. }
  789. return _skipBtn;
  790. }
  791. - (NSString *)appId {
  792. return @"3299576";
  793. }
  794. @end