RQADViewManager.m 32 KB

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