RQADViewManager.m 32 KB

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