RQADViewManager.m 32 KB

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