RQADViewManager.m 33 KB

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