RQADViewManager.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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 <AppTrackingTransparency/AppTrackingTransparency.h>
  10. #import <AdSupport/AdSupport.h>
  11. #import <ADSuyiSDK/ADSuyiSDK.h>
  12. #import <ADSuyiSDK/ADSuyiSDKSplashAd.h>
  13. #import <ADSuyiKit/UIColor+ADSuyiKit.h>
  14. #import <ADSuyiKit/ADSuyiKitMacros.h>
  15. #import <ADSuyiKit/ADSuyiKitLogging.h>
  16. #import "YYTimer.h"
  17. // 支持广点通开屏广告v+
  18. #define SUPPORT_SPLASH_ZOOMOUT 1
  19. #if defined(__IPHONE_14_0)
  20. #import <AppTrackingTransparency/AppTrackingTransparency.h>//适配iOS14
  21. #endif
  22. static RQADViewManager *manger = nil;
  23. static dispatch_once_t onceToken;
  24. static NSString *appId = @"3206311";
  25. static NSString *splashPlcId = @"7baf358edd3600ff50";
  26. static NSString *bannerPlcId = @"f2889056c5a7331f1e";
  27. static NSString *nativeAdPlcId = @"2d2e5b5eb5d2ba10c6";
  28. static NSString *homePageNativeAdPlcId = @"bd37f94cd1cf693488";
  29. static NSString *fullScreenvodAdPlcId = @"2f9dd9678f50239cdb";
  30. static NSString *intertitialAdPlcId = @"dbdadc0f30655a8d7e";
  31. //插屏开屏广告
  32. static NSString *intertitialSplashAdPlcId = @"8f2d81c8b6275b6d80";
  33. @interface RQADViewManager () <ADSuyiSDKBannerAdViewDelegate, ADSuyiSDKNativeAdDelegate,ADSuyiSDKFullScreenVodAdDelegate,ADSuyiSDKIntertitialAdDelegate,
  34. #ifdef SUPPORT_SPLASH_ZOOMOUT
  35. ADSuyiSDKSplashAdZoomOutViewDelegate,
  36. #endif
  37. ADSuyiSDKSplashAdDelegate
  38. >
  39. /* 插屏广告 */
  40. @property (nonatomic, readwrite, strong) ADSuyiSDKIntertitialAd *intertitialAd;
  41. /* 开屏广告对象 */
  42. @property (nonatomic, readwrite, strong) ADSuyiSDKSplashAd *splashAd;
  43. /* 横幅广告 */
  44. @property (nonatomic, readwrite, strong) ADSuyiSDKBannerAdView *bannerView;
  45. /* 信息流广告 */
  46. @property (nonatomic, readwrite, strong) ADSuyiSDKNativeAd *nativeAd;
  47. /* 全屏视频广告 */
  48. @property (nonatomic, readwrite, strong) ADSuyiSDKFullScreenVodAd *fullScreenvodAd;
  49. @property (nonatomic, readwrite, strong) UIView<ADSuyiAdapterNativeAdViewDelegate> *currentAdView;
  50. @property (nonatomic, readwrite, strong) UIView *customView;
  51. @property (nonatomic ,strong) dispatch_source_t myTimer;// 注意:此处应该使用强引用 strong
  52. @property (nonatomic, readwrite, assign) BOOL isResume;
  53. @property (nonatomic, readwrite, assign) RQADType adType;
  54. @property (nonatomic, readwrite, strong) RQADCloseCompletedBlock aDCloseCompletedBlock;
  55. @property (nonatomic, readwrite, strong) UIButton *skipBtn;
  56. @property (nonatomic, readwrite, strong) YYTimer *timer;
  57. @property (nonatomic, readwrite, assign) NSUInteger count;
  58. @property (nonatomic, readwrite, assign) BOOL isReady;
  59. @property (nonatomic, readwrite, assign) BOOL isCanShow;
  60. @end
  61. @implementation RQADViewManager
  62. #pragma mark - SystemMethod
  63. + (RQADViewManager *)sharedManager {
  64. dispatch_once(&onceToken, ^{
  65. manger = [[self alloc] init];
  66. });
  67. return manger;
  68. }
  69. - (instancetype)init {
  70. self = [super init];
  71. if (self) {
  72. [self initBaseData];
  73. }
  74. return self;
  75. }
  76. - (void)dealloc {
  77. [self stopTimer];
  78. }
  79. #pragma mark - PublicMethods
  80. - (void)loadAdWithAdType:(RQADType)adType customView:(UIView * _Nullable )customView {
  81. [self loadAdWithAdType:adType customView:customView cycleSecound:0.f];
  82. }
  83. - (void)loadAdWithAdType:(RQADType)adType customView:(UIView * _Nullable )customView cycleSecound:(CGFloat)second {
  84. self.adType = adType;
  85. self.customView = customView;
  86. /// 服务端允许展示广告
  87. // if (RQ_SHARE_FUNCTION.APP_AD) {
  88. //
  89. // }
  90. switch (adType) {
  91. case RQADType_Splash: {
  92. /// 不展示广告在有引导页的时候
  93. // if (!myDelegate.adIsShow) {
  94. // [self setThirtyPartySdk];
  95. // }
  96. [self setThirtyPartySdk];
  97. break;
  98. }
  99. case RQADType_Banner: {
  100. [self.bannerView removeFromSuperview];
  101. self.bannerView.delegate = nil;
  102. self.bannerView = nil;
  103. [self.customView addSubview:self.bannerView];
  104. [self.bannerView loadAndShowWithError:nil];
  105. break;
  106. }
  107. case RQADType_native:
  108. case RQADType_HomePage_native:{
  109. if (self.currentAdView) {
  110. [self.currentAdView removeFromSuperview];
  111. self.currentAdView = nil;
  112. self.nativeAd = nil;
  113. }
  114. [self.nativeAd load:1];
  115. }
  116. break;
  117. case RQADType_fullScreen: {
  118. [self.fullScreenvodAd loadAdData];
  119. }
  120. break;
  121. case RQADType_Interstitial: {
  122. self.isReady = NO;
  123. _intertitialAd = nil;
  124. [self.intertitialAd loadAdData];
  125. }
  126. break;
  127. case RQADType_InterstitialSplash: {
  128. [self setThirtyPartySdk];
  129. }
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. - (void)showAdWithAdType:(RQADType)adType {
  136. switch (adType) {
  137. case RQADType_Interstitial: {
  138. if (self.isReady) {
  139. self.isCanShow = YES;
  140. [self.intertitialAd show];
  141. }
  142. }
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. - (void)initCloseBlock:(RQADCloseCompletedBlock)block {
  149. self.aDCloseCompletedBlock = block;
  150. }
  151. #pragma mark - Action
  152. - (void)_timerValueChanged:(YYTimer *)timer {
  153. self.count--;
  154. if (self.count == 0) {
  155. [timer invalidate];
  156. self.timer = nil;
  157. [self.skipBtn setTitleNormal:@"跳过"];
  158. if (self.currentAdView) {
  159. [self.currentAdView adsy_close];
  160. }
  161. return;
  162. }
  163. [self.skipBtn setTitleNormal:[NSString stringWithFormat:@"跳过 %zd", self.count]];
  164. }
  165. #pragma mark - PrivateMethods
  166. - (void)setThirtyPartySdk {
  167. if ([self isFirstAppLoad]) {
  168. [self showAgreePrivacy];
  169. }else {
  170. [self initADSuyiSDK];
  171. }
  172. }
  173. - (void)initADSuyiSDK {
  174. @weakify(self)
  175. dispatch_async(dispatch_get_main_queue(), ^{
  176. @strongify(self)
  177. // 设置日志输出等级
  178. [ADSuyiSDK setLogLevel:ADSuyiKitLogLevelDebug];
  179. // ADSuyiSDK初始化
  180. [ADSuyiSDK initWithAppId:appId completionBlock:^(NSError * _Nonnull error) {
  181. if (error) {
  182. NSLog(@"SDK 初始化失败:%@", error.localizedDescription);
  183. }
  184. }];
  185. /*
  186. * 推荐在AppDelegate中的最后加载开屏广告
  187. * 其他的接入方式会有需要特殊注意的方式,遇到过的相关问题在SDK相关问题的文档中有提到
  188. */
  189. if (_adType == RQADType_InterstitialSplash) {
  190. self.isReady = NO;
  191. _intertitialAd = nil;
  192. [self.intertitialAd loadAdData];
  193. } else {
  194. [self loadSplashAd];
  195. }
  196. });
  197. }
  198. - (void)showAgreePrivacy {
  199. @weakify(self)
  200. // 隐私合规化示例
  201. // 隐私合规化示例
  202. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  203. // app名称
  204. NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
  205. NSString *messageStr = [NSString stringWithFormat:@"亲爱的用户,欢迎您信任并使用【%@】,我们依据相关法律制定了《用户协议》和《隐私协议》帮你你了解我们手机,使用,存储和共享个人信息情况,请你在点击之前仔细阅读并理解相关条款。\n1、在使用我们的产品和服务时,将会提供与具体功能有关的个法人信息(可能包括身份验证,位置信息,设备信息和操作日志等)\n2、我们会采用业界领先的安全技术来保护你的个人隐私,未经授权许可我们不会讲上述信息共享给任何第三方或用于未授权的其他用途。\n如你同意请点击同意按钮并继续。",app_Name];
  206. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"温馨提示" message:messageStr preferredStyle:(UIAlertControllerStyleAlert)];
  207. UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"不同意" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  208. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"点击同意才能使用该App服务" preferredStyle:(UIAlertControllerStyleAlert)];
  209. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
  210. [RQControllerHelper.currentViewController presentViewController:alertVc animated:YES completion:nil];
  211. }];
  212. [alert addAction:sure];
  213. [RQControllerHelper.currentViewController presentViewController:alert animated:YES completion:nil];
  214. }];
  215. UIAlertAction *agree = [UIAlertAction actionWithTitle:@"同意并继续" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  216. @strongify(self)
  217. // 记录是否第一次启动
  218. [self writeAppLoad];
  219. // 获取idfa权限 建议启动就获取权限 不获取权限会影响收益
  220. if (@available(iOS 14.0, *)) {
  221. [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
  222. }];
  223. }
  224. // 用户同意隐私后 初始化
  225. [self initADSuyiSDK];
  226. }];
  227. [alertVc addAction:cancle];
  228. [alertVc addAction:agree];
  229. [RQControllerHelper.currentViewController presentViewController:alertVc animated:YES completion:nil];
  230. }
  231. #pragma mark -- helper
  232. - (void)writeAppLoad {
  233. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  234. [userDefault setObject:@"yes" forKey:@"isFirstLoad"];
  235. [userDefault synchronize];
  236. }
  237. - (BOOL)isFirstAppLoad {
  238. NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  239. if ([[userDefault objectForKey:@"isFirstLoad"] isEqualToString:@"yes"]) {
  240. return NO;
  241. }
  242. return YES;
  243. }
  244. - (void)loadSplashAd {
  245. if (self.splashAd) {
  246. return;
  247. }
  248. // 1、初始化开屏广告实例对象
  249. self.splashAd = [[ADSuyiSDKSplashAd alloc]init];
  250. self.splashAd.delegate = self;
  251. #ifdef SUPPORT_SPLASH_ZOOMOUT
  252. self.splashAd.zoomOutViewDelegate = self;
  253. #endif
  254. self.splashAd.controller = RQControllerHelper.currentViewController;
  255. // 2、设置开屏的广告位id
  256. self.splashAd.posId = splashPlcId;
  257. /**
  258. 开屏请求总超时时间:所有平台轮询的请求等待总时长(不包括图片渲染时间),单位秒,推荐设置为4s,最小值为3s
  259. 开屏各平台分配逻辑:(目前只有开屏需要分配时间,并且理论上分配给到各平台的超时时间不会完全耗尽)
  260. 1、3<=tolerateTimeout<=4:轮询首位平台的超时时间为(tolerateTimeout-1)s,次位为2s,如果后续还有平台统一为1s;
  261. 2、tolerateTimeout>=5:轮询首位平台的超时时间为(tolerateTimeout-2)s,次位为3s,如果后续还有平台统一为2s;
  262. */
  263. self.splashAd.tolerateTimeout = 4;
  264. // 3、设置默认启动图(一般设置启动图的平铺颜色为背景颜色,使得视觉效果更加平滑)
  265. UIViewController * vc = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
  266. CGRect rect = [UIScreen mainScreen].bounds;
  267. UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
  268. CGContextRef context = UIGraphicsGetCurrentContext();
  269. vc.view.frame = rect;
  270. [vc.view.layer renderInContext:context];
  271. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  272. UIGraphicsEndImageContext();
  273. self.splashAd.backgroundColor = [UIColor adsy_getColorWithImage:image withNewSize:[UIScreen mainScreen].bounds.size];
  274. // 4、开屏广告机型适配
  275. CGFloat bottomViewHeight;
  276. // if (kADSYCurveScreen) { // 刘海屏
  277. bottomViewHeight = [UIScreen mainScreen].bounds.size.height * 0.15;
  278. // } else {
  279. // bottomViewHeight = [UIScreen mainScreen].bounds.size.height - [UIScreen mainScreen].bounds.size.width * (960 / 640.0);
  280. // }
  281. // 6、设置开屏保底逻辑(可选)
  282. /**
  283. *功能说明:App在首次启动时,需要先请求获取广告位配置文件后,然后再去请求开屏广告,也就是首次加载开屏广告时需要两次串行网络请求,因此很容易因超时导致开屏广告展示失败。
  284. *解决方案:为避免开屏超时问题,开放此设置给开发者,开发者可以根据实际需求选择一家广告平台,通过API接口将必需参数传递给Suyi聚合SDK。(该设置只能指定一家广告平台,并且首次启动时只会请求该平台的广告,但App首次开屏广告将不受ADmobile后台控制,包括下载提示,广告位关闭。)
  285. *该设置仅会在首次加载开屏广告时,SDK会使用开发者传入的参数进行广告请求,同时获取后台配置文件的广告配置信息缓存到本地(首次请求广告平台广告和获取配置信息时并发进行),后续的开屏广告将按照缓存缓存的后台广告位配置顺序进行开屏广告请求。
  286. *支持穿山甲、优量汇、快手、百度
  287. */
  288. [self.splashAd setBottomSplashWithSuyiPosid:splashPlcId platformListId:@"5237499" platform:@"csjad" appId:@"5237499" appKey:nil platformPosid:@"887634083" renderType:ADSuyiSplashRenderTypeExpressPro];
  289. // 7、加载开屏广告
  290. [self.splashAd loadAndShowInWindow:RQ_APPDELEGATE.window];
  291. }
  292. - (void)initBaseData {
  293. [[[[[RQNotificationCenter rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] takeUntil:self.rac_willDeallocSignal] map:^id(NSNotification *value) {
  294. return value.object;
  295. }] distinctUntilChanged] subscribeNext:^(id x) {
  296. NSLog(@"%@",@"APP进入后台");
  297. }];
  298. }
  299. //获取当前屏幕显示的viewcontroller
  300. - (UIViewController *)getCurrentVC {
  301. UIViewController *resultVC;
  302. resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
  303. /// RQ Fixed : 这里必须要判断一下,否则取出来永远都是 RQMainTabBarViewController。这是架构上小缺(特)陷(性)。因为RQMainTabBarViewController的子控制器是UITabBarController,所以需要递归UITabBarController的所有的子控制器
  304. if ([resultVC isKindOfClass:[RQTableViewController class]]) {
  305. RQTableViewController *mainVc = (RQTableViewController *)resultVC;
  306. resultVC = [self _topViewController:mainVc.tabBarController];
  307. }
  308. return resultVC;
  309. }
  310. - (UIViewController *)_topViewController:(UIViewController *)vc {
  311. if ([vc isKindOfClass:[UINavigationController class]]) {
  312. return [self _topViewController:[(UINavigationController *)vc topViewController]];
  313. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  314. return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
  315. } else {
  316. return vc;
  317. }
  318. }
  319. - (void)initTimerWithSecound:(CGFloat)second {
  320. //2.设置时间等
  321. /*
  322. 第一个参数:定时器对象
  323. 第二个参数:DISPATCH_TIME_NOW 表示从现在开始计时
  324. 第三个参数:间隔时间 GCD里面的时间最小单位为 纳秒
  325. 第四个参数:精准度(表示允许的误差,0表示绝对精准)
  326. */
  327. dispatch_source_set_timer(self.myTimer, DISPATCH_TIME_NOW, second * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
  328. //3.要调用的任务
  329. dispatch_source_set_event_handler(self.myTimer, ^{
  330. NSLog(@"GCD-----%@",[NSThread currentThread]);
  331. // [self.bannerAdManager loadNativeAd];
  332. // if (![[self getCurrentVC] isKindOfClass:[ExerciseVC class]]) {
  333. // NSLog(@"%s",object_getClassName([self getCurrentVC]));
  334. // [self stopTimer];
  335. // }
  336. });
  337. }
  338. - (void)pauseTimer {
  339. if (self.myTimer) {
  340. dispatch_suspend(_myTimer);
  341. }
  342. }
  343. - (void)resumeTimer {
  344. if (_myTimer) {
  345. dispatch_resume(_myTimer);
  346. self.isResume = YES;
  347. }
  348. }
  349. - (void)stopTimer {
  350. self.isResume = NO;
  351. if (_myTimer) {
  352. dispatch_source_cancel(_myTimer);
  353. _myTimer = nil;
  354. }
  355. }
  356. #pragma mark - ADSuyiSDKSplashAdDelegate
  357. /**
  358. 开屏展现成功
  359. @param splashAd 广告实例
  360. */
  361. - (void)adsy_splashAdSuccessToPresentScreen:(ADSuyiSDKSplashAd *)splashAd{
  362. #ifdef SUPPORT_SPLASH_ZOOMOUT
  363. if(splashAd.splashZoomOutView) {
  364. UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
  365. [rootVc.view addSubview:splashAd.splashZoomOutView];
  366. }
  367. #endif
  368. }
  369. /**
  370. 开屏展现失败
  371. @param splashAd 广告实例
  372. @param error 具体错误信息
  373. */
  374. - (void)adsy_splashAdFailToPresentScreen:(ADSuyiSDKSplashAd *)splashAd failToPresentScreen:(ADSuyiAdapterErrorDefine *)error{
  375. _splashAd = nil;
  376. }
  377. /**
  378. 开屏广告点击
  379. @param splashAd 广告实例
  380. */
  381. - (void)adsy_splashAdClicked:(ADSuyiSDKSplashAd *)splashAd{
  382. }
  383. /**
  384. 开屏被关闭
  385. @param splashAd 广告实例
  386. */
  387. - (void)adsy_splashAdClosed:(ADSuyiSDKSplashAd *)splashAd{
  388. #ifdef SUPPORT_SPLASH_ZOOMOUT
  389. if(_splashAd.splashZoomOutView == nil)
  390. _splashAd = nil;
  391. #else
  392. _splashAd = nil;
  393. #endif
  394. }
  395. /**
  396. 开屏展示
  397. @param splashAd 广告实例
  398. */
  399. - (void)adsy_splashAdEffective:(ADSuyiSDKSplashAd *)splashAd{
  400. }
  401. #ifdef SUPPORT_SPLASH_ZOOMOUT
  402. #pragma mark - ADSuyiSDKSplashAdZoomOutViewDelegate
  403. /**
  404. ZoomOutView被点击
  405. @param splashAd 广告实例
  406. @param splashZoomOutView zoomOutView
  407. */
  408. - (void)adsy_splashZoomOutViewDidClick:(ADSuyiSDKSplashAd *)splashAd
  409. splashZoomOutView:(__kindof UIView <ADSuyiSDKSplashAdZoomOutViewProtocol>*)splashZoomOutView {
  410. }
  411. /**
  412. ZoomOutView 被关闭
  413. @param splashAd 广告实例
  414. @param splashZoomOutView zoomOutView
  415. */
  416. - (void)adsy_splashZoomOutViewAdDidClose:(ADSuyiSDKSplashAd *)splashAd
  417. splashZoomOutView:(__kindof UIView <ADSuyiSDKSplashAdZoomOutViewProtocol>*)splashZoomOutView {
  418. }
  419. /**
  420. ZoomOutView 播放完成
  421. @param splashAd 广告实例
  422. @param splashZoomOutView zoomOutView
  423. */
  424. - (void)adsy_splashZoomOutViewAdVideoFinished:(ADSuyiSDKSplashAd *)splashAd
  425. splashZoomOutView:(__kindof UIView <ADSuyiSDKSplashAdZoomOutViewProtocol>*)splashZoomOutView {
  426. }
  427. /**
  428. ZoomOutView 进入视频详情页
  429. @param splashAd 广告实例
  430. @param splashZoomOutView zoomOutView
  431. */
  432. - (void)adsy_splashZoomOutViewAdDidPresentFullScreenModal:(ADSuyiSDKSplashAd *)splashAd
  433. splashZoomOutView:(__kindof UIView <ADSuyiSDKSplashAdZoomOutViewProtocol>*)splashZoomOutView {
  434. }
  435. /**
  436. ZoomOutView 退出视频详情页
  437. @param splashAd 广告实例
  438. @param splashZoomOutView zoomOutView
  439. */
  440. - (void)adsy_splashZoomOutViewAdDidDismissFullScreenModal:(ADSuyiSDKSplashAd *)splashAd
  441. splashZoomOutView:(__kindof UIView <ADSuyiSDKSplashAdZoomOutViewProtocol>*)splashZoomOutView {
  442. _splashAd = nil;
  443. }
  444. #endif
  445. #pragma mark - ADSuyiSDKBannerAdViewDelegate
  446. /**
  447. 广告获取成功
  448. @param bannerView banner实例
  449. */
  450. - (void)adsy_bannerViewDidReceived:(ADSuyiSDKBannerAdView *)bannerView{
  451. }
  452. /**
  453. 广告拉取失败
  454. @param bannerView banner实例
  455. @param errorModel 错误描述
  456. */
  457. - (void)adsy_bannerViewFailToReceived:(ADSuyiSDKBannerAdView *)bannerView errorModel:(ADSuyiAdapterErrorDefine *)errorModel{
  458. NSLog(@"adsy_bannerViewFailToReceived:%@, %@",errorModel.errorDescription, errorModel.errorDetailDict);
  459. [_bannerView removeFromSuperview];
  460. _bannerView = nil;
  461. }
  462. /**
  463. 广告点击
  464. @param bannerView 广告实例
  465. */
  466. - (void)adsy_bannerViewClicked:(ADSuyiSDKBannerAdView *)bannerView{
  467. }
  468. /**
  469. 广告关闭
  470. @param bannerView 广告实例
  471. */
  472. - (void)adsy_bannerViewClose:(ADSuyiSDKBannerAdView *)bannerView{
  473. _bannerView = nil;
  474. }
  475. /**
  476. 广告展示
  477. @param bannerView 广告实例
  478. */
  479. - (void)adsy_bannerViewExposure:(ADSuyiSDKBannerAdView *)bannerView{
  480. }
  481. #pragma mark - ADSuyiSDKNativeAdDelegate
  482. - (void)adsy_nativeAdSucessToLoad:(ADSuyiSDKNativeAd *)nativeAd
  483. adViewArray:(NSArray<__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *> *)adViewArray {
  484. for (UIView<ADSuyiAdapterNativeAdViewDelegate> *adView in adViewArray) {
  485. // 4、判断信息流广告是否为自渲染类型(可选实现) 可仿照所示样式demo实现 如无所需样式则需自行实现
  486. // 如果单纯只配置了模版信息流,那么不需要实现,如果配置了自渲染信息流,那么需要实现
  487. if(adView.renderType == ADSuyiAdapterRenderTypeNative) {
  488. // 4.1、如果是自渲染类型则可样式自定义(3种示例demo样式见下)
  489. // 1、常规样式
  490. // [self setUpUnifiedTopImageNativeAdView:adView];
  491. // 2、纯图样式
  492. [self setUpUnifiedOnlyImageNativeAdView:adView];
  493. // 3、上图下文
  494. // [self setUpUnifiedTopImageNativeAdView:adView];
  495. }
  496. // 5、注册,自渲染:注册点击事件,模板:render,重要
  497. [adView adsy_registViews:@[adView]];
  498. // 广点通视频信息流广告会给mediaView添加事件,点击会出现半屏广告,以下为广点通官方给予的解决方案
  499. if([adView.adsy_platform isEqualToString:ADSuyiAdapterPlatformGDT]
  500. && adView.renderType == ADSuyiAdapterRenderTypeNative
  501. && adView.data.shouldShowMediaView) {
  502. UIView *mediaView = [adView adsy_mediaViewForWidth:0.0];
  503. mediaView.userInteractionEnabled = NO;
  504. }
  505. }
  506. }
  507. - (void)adsy_nativeAdFailToLoad:(ADSuyiSDKNativeAd *)nativeAd
  508. errorModel:(ADSuyiAdapterErrorDefine *)errorModel {
  509. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Faild) : nil;
  510. }
  511. - (void)adsy_nativeAdViewRenderOrRegistSuccess:(UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  512. self.currentAdView = adView;
  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. [adView adsy_close];
  523. }
  524. - (void)adsy_nativeAdClose:(ADSuyiSDKNativeAd *)nativeAd
  525. adView:(__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  526. if (self.adType == RQADType_native) {
  527. if (self.currentAdView) {
  528. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Close) : nil;
  529. self.currentAdView = adView;
  530. [self.currentAdView removeFromSuperview];
  531. [self.skipBtn removeFromSuperview];
  532. self.currentAdView = nil;
  533. self.nativeAd = nil;
  534. }
  535. } else {
  536. if (self.currentAdView) {
  537. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Close) : nil;
  538. self.currentAdView = adView;
  539. [self.currentAdView removeFromSuperview];
  540. self.currentAdView = nil;
  541. self.nativeAd = nil;
  542. }
  543. }
  544. }
  545. - (void)adsy_nativeAdExposure:(ADSuyiSDKNativeAd *)nativeAd
  546. adView:(__kindof UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  547. if (self.adType == RQADType_native) {
  548. self.currentAdView = adView;
  549. [self.customView bringSubviewToFront:self.skipBtn];
  550. self.count = 5;
  551. [_skipBtn setTitleNormal:@"跳过 5"];
  552. self.timer = [YYTimer timerWithTimeInterval:1 target:self selector:@selector(_timerValueChanged:) repeats:YES];
  553. self.aDCloseCompletedBlock? self.aDCloseCompletedBlock(RQADDoType_Success) : nil;
  554. }
  555. }
  556. #pragma mark - Helper 自渲染类型信息流处理方法 setUpUnifiedOnlyImageNativeAdView纯图样式
  557. // 纯图样式
  558. - (void)setUpUnifiedOnlyImageNativeAdView:(UIView<ADSuyiAdapterNativeAdViewDelegate> *)adView {
  559. // 设计的adView实际大小,其中宽度和高度可以自己根据自己的需求设置
  560. CGFloat adWidth = self.customView.frame.size.width;
  561. CGFloat adHeight = adWidth / 16.0 * 9;
  562. adView.frame = CGRectMake(0, 0, adWidth, adHeight);
  563. // 设置主图/视频(主图可选,但强烈建议带上,如果有视频试图,则必须带上)
  564. CGRect mainFrame = CGRectMake(0, 0, adWidth, adHeight);
  565. if(adView.data.shouldShowMediaView) {
  566. UIView *mediaView = [adView adsy_mediaViewForWidth:mainFrame.size.width];
  567. mediaView.frame = mainFrame;
  568. [adView addSubview:mediaView];
  569. } else {
  570. UIImageView *imageView = [UIImageView new];
  571. imageView.backgroundColor = [UIColor adsy_colorWithHexString:@"#CCCCCC"];
  572. [adView addSubview:imageView];
  573. imageView.frame = mainFrame;
  574. NSString *urlStr = adView.data.imageUrl;
  575. if(urlStr.length > 0) {
  576. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
  577. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]];
  578. dispatch_async(dispatch_get_main_queue(), ^{
  579. imageView.image = image;
  580. });
  581. });
  582. }
  583. }
  584. // 展示关闭按钮(必要)
  585. [self.customView addSubview:self.skipBtn];
  586. self.skipBtn.frame = CGRectMake(adWidth - 12 - 54, 12, 54, 27);
  587. [self.skipBtn addTarget:adView action:@selector(adsy_close) forControlEvents:UIControlEventTouchUpInside];
  588. // 显示logo图片(必要)
  589. if(![adView.adsy_platform isEqualToString:ADSuyiAdapterPlatformGDT]) { // 优量汇(广点通)会自带logo,不需要添加
  590. UIImageView *logoImage = [UIImageView new];
  591. [adView addSubview:logoImage];
  592. // [adView bringSubviewToFront:logoImage];
  593. [adView adsy_platformLogoImageDarkMode:NO loadImageBlock:^(UIImage * _Nullable image) {
  594. CGFloat maxWidth = 40;
  595. CGFloat logoHeight = maxWidth / image.size.width * image.size.height;
  596. logoImage.frame = CGRectMake(adWidth - maxWidth, adHeight - logoHeight, maxWidth, logoHeight);
  597. logoImage.image = image;
  598. }];
  599. }
  600. }
  601. #pragma mark - ADSuyiSDKFullScreenVodAdDelegate
  602. /**
  603. ADSuyiSDKFullScreenVodAd请求成功回调
  604. @param fullScreenVodAd 全屏视频广告实例对象
  605. */
  606. - (void)adsy_fullScreenVodAdSuccedToLoad:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd{
  607. }
  608. /**
  609. ADSuyiSDKFullScreenVodAd准备好被播放
  610. @param fullScreenVodAd 全屏视频广告实例对象
  611. */
  612. - (void)adsy_fullScreenVodAdReadyToPlay:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd{
  613. // 3、推荐在准备好被播放会调用展示激励视频广告
  614. [_fullScreenvodAd show];
  615. }
  616. /**
  617. ADSuyiSDKFullScreenVodAd视频加载成功回调
  618. @param fullScreenVodAd 全屏视频广告实例对象
  619. */
  620. - (void)adsy_fullScreenVodAdSuccessToLoadVideo:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd{
  621. // 3、也可在视频加载成功回调中展示激励视频广告
  622. }
  623. /**
  624. ADSuyiSDKFullScreenVodAd请求失败回调
  625. @param fullScreenVodAd 全屏视频广告实例对象
  626. @param error 失败原因
  627. */
  628. - (void)adsy_fullScreenVodAdFailedToLoad:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd error:(ADSuyiAdapterErrorDefine *)error{
  629. // 4、广告内存回收
  630. _fullScreenvodAd = nil;
  631. }
  632. /**
  633. ADSuyiSDKFullScreenVodAd展示在屏幕内回调
  634. @param fullScreenVodAd 全屏视频广告实例对象
  635. */
  636. - (void)adsy_fullScreenVodAdDidPresent:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd{
  637. }
  638. /**
  639. ADSuyiSDKFullScreenVodAd展示失败回调
  640. @param fullScreenVodAd 全屏视频广告实例对象
  641. @param error 失败原因
  642. */
  643. - (void)adsy_fullScreenVodAdFailToPresent:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd error:(NSError *)error{
  644. }
  645. /**
  646. ADSuyiSDKFullScreenVodAd点击回调
  647. @param fullScreenVodAd 全屏视频广告实例对象
  648. */
  649. - (void)adsy_fullScreenVodAdDidClick:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd{
  650. }
  651. /**
  652. ADSuyiSDKFullScreenVodAd关闭回调
  653. @param fullScreenVodAd 全屏视频广告实例对象
  654. */
  655. - (void)adsy_fullScreenVodAdDidClose:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd{
  656. // 4、广告内存回收
  657. _fullScreenvodAd = nil;
  658. }
  659. /**
  660. ADSuyiSDKFullScreenVodAd展示回调
  661. @param fullScreenVodAd 广告实例
  662. */
  663. - (void)adsy_fullScreenVodAdExposure:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd{
  664. }
  665. /**
  666. ADSuyiSDKFullScreenVodAd播放完成回调
  667. @param fullScreenVodAd 广告实例
  668. @param error 失败原因(如果为nil,则是单纯的播放完成)
  669. */
  670. - (void)adsy_fullScreenVodAdPlayComplete:(ADSuyiSDKFullScreenVodAd *)fullScreenVodAd didFailed:(NSError *_Nullable)error{
  671. }
  672. #pragma mark - ADSuyiSDKIntertitialAdDelegate
  673. /**
  674. ADSuyiSDKIntertitialAd请求成功回调
  675. @param interstitialAd 插屏广告实例对象
  676. */
  677. - (void)adsy_interstitialAdSuccedToLoad:(ADSuyiSDKIntertitialAd *)interstitialAd {
  678. self.isReady = YES;
  679. if (!RQ_Video_Module.playerController.isPlaying && self.adType == RQADType_Interstitial && self.isCanShow && _intertitialAd) {
  680. [_intertitialAd show];
  681. }
  682. }
  683. /**
  684. ADSuyiSDKIntertitialAd请求失败回调
  685. @param interstitialAd 插屏广告实例对象
  686. @param error 失败原因
  687. */
  688. - (void)adsy_interstitialAdFailedToLoad:(ADSuyiSDKIntertitialAd *)interstitialAd error:(ADSuyiAdapterErrorDefine *)error {
  689. _intertitialAd = nil;
  690. }
  691. /**
  692. ADSuyiSDKIntertitialAd展示在屏幕内回调
  693. @param interstitialAd 插屏广告实例对象
  694. */
  695. - (void)adsy_interstitialAdDidPresent:(ADSuyiSDKIntertitialAd *)interstitialAd {
  696. }
  697. /**
  698. ADSuyiSDKIntertitialAd展示在屏幕内失败回调
  699. @param interstitialAd 插屏广告实例对象
  700. */
  701. - (void)adsy_interstitialAdFailedToPresent:(ADSuyiSDKIntertitialAd *)interstitialAd error:(NSError *)error {
  702. }
  703. /**
  704. ADSuyiSDKIntertitialAd点击回调
  705. @param interstitialAd 插屏广告实例对象
  706. */
  707. - (void)adsy_interstitialAdDidClick:(ADSuyiSDKIntertitialAd *)interstitialAd {
  708. }
  709. /**
  710. ADSuyiSDKIntertitialAd关闭回调
  711. @param interstitialAd 插屏广告实例对象
  712. */
  713. - (void)adsy_interstitialAdDidClose:(ADSuyiSDKIntertitialAd *)interstitialAd {
  714. }
  715. /**
  716. ADSuyiSDKIntertitialAd展示回调
  717. @param interstitialAd 广告实例
  718. */
  719. - (void)adsy_interstitialAdExposure:(ADSuyiSDKIntertitialAd *)interstitialAd {
  720. }
  721. #pragma mark - LazyLoad
  722. - (dispatch_source_t)myTimer {
  723. if (!_myTimer) {
  724. //0.创建队列
  725. dispatch_queue_t queue = dispatch_get_main_queue();
  726. //1.创建GCD中的定时器
  727. /*
  728. 第一个参数:创建source的类型 DISPATCH_SOURCE_TYPE_TIMER:定时器
  729. 第二个参数:0
  730. 第三个参数:0
  731. 第四个参数:队列
  732. */
  733. _myTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  734. }
  735. return _myTimer;
  736. }
  737. - (UIView *)customView {
  738. if (!_customView) {
  739. CGRect rect;
  740. switch (self.adType) {
  741. case RQADType_Splash:
  742. rect = CGRectMake(0, 0, RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT * 0.75);
  743. break;
  744. case RQADType_Banner:
  745. rect = CGRectMake(25, (RQ_SCREEN_HEIGHT - RQ_APPLICATION_TOP_BAR_HEIGHT - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT) - 60 - 40, RQ_SCREEN_WIDTH - 50, 50);
  746. default:
  747. rect = CGRectMake(0, 0, RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT);
  748. break;
  749. }
  750. _customView = [[UIView alloc] initWithFrame:rect];
  751. }
  752. return _customView;
  753. }
  754. - (ADSuyiSDKBannerAdView *)bannerView {
  755. if (!_bannerView) {
  756. // 1、初始化banner视图,并给定frame值,rate取值根据banner的尺寸
  757. _bannerView = [[ADSuyiSDKBannerAdView alloc] initWithFrame:CGRectMake(0, 0, RQ_SCREEN_WIDTH - 50, (RQ_SCREEN_WIDTH - 50) / (640/100.0))];
  758. _bannerView.delegate = self;
  759. // 2、设置控制器,用来弹出落地页,重要
  760. _bannerView.controller = RQControllerHelper.currentViewController;
  761. // 3、设置广告位id,重要
  762. _bannerView.posId = bannerPlcId;
  763. // 可设置bannerview的背景色,请按需添加修改该行代码
  764. _bannerView.backgroundColor = UIColor.whiteColor;
  765. }
  766. return _bannerView;
  767. }
  768. - (ADSuyiSDKNativeAd *)nativeAd {
  769. if (!_nativeAd) {
  770. _nativeAd = [[ADSuyiSDKNativeAd alloc] initWithAdSize:self.customView.size];
  771. _nativeAd.delegate = self;
  772. _nativeAd.controller = RQControllerHelper.currentViewController;
  773. _nativeAd.posId = (self.adType == RQADType_HomePage_native)? homePageNativeAdPlcId : nativeAdPlcId;
  774. }
  775. return _nativeAd;
  776. }
  777. - (ADSuyiSDKFullScreenVodAd *)fullScreenvodAd {
  778. if (!_fullScreenvodAd) {
  779. _fullScreenvodAd = [ADSuyiSDKFullScreenVodAd new];
  780. _fullScreenvodAd.controller = RQControllerHelper.currentViewController;
  781. _fullScreenvodAd.posId = fullScreenvodAdPlcId;
  782. _fullScreenvodAd.delegate = self;
  783. _fullScreenvodAd.tolerateTimeout = 4;
  784. }
  785. return _fullScreenvodAd;
  786. }
  787. - (ADSuyiSDKIntertitialAd *)intertitialAd {
  788. if (!_intertitialAd) {
  789. _intertitialAd = [ADSuyiSDKIntertitialAd new];
  790. UIViewController *vc = RQControllerHelper.currentViewController;
  791. _intertitialAd.controller = vc;
  792. _intertitialAd.posId = (_adType == RQADType_InterstitialSplash)? intertitialAdPlcId : intertitialAdPlcId;
  793. _intertitialAd.delegate = self;
  794. _intertitialAd.tolerateTimeout = 4;
  795. _intertitialAd.adSize = CGSizeMake(vc.view.frame.size.width, vc.view.frame.size.height);
  796. }
  797. return _intertitialAd;
  798. }
  799. - (UIButton *)skipBtn {
  800. if (!_skipBtn) {
  801. _skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  802. _skipBtn.frame = CGRectMake(RQ_SCREEN_WIDTH - 54 - 12, 12, 54, 27);
  803. [_skipBtn setBackgroundColor:[UIColor colorWithWhite:0.1 alpha:0.3]];
  804. // [_skipBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.1 alpha:1] size:CGSizeMake(200, 200)] forState:UIControlStateNormal];
  805. _skipBtn.titleLabel.font = RQRegularFont(13);
  806. [_skipBtn setTitleNormal:@"跳过 5"];
  807. _skipBtn.layer.cornerRadius = 13.5;
  808. _skipBtn.clipsToBounds = YES;
  809. }
  810. return _skipBtn;
  811. }
  812. @end