GMAdManager.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // GMAdManager.m
  3. // Moriss
  4. //
  5. // Created by Mgs on 2020/8/22.
  6. //
  7. #import "GMAdManager.h"
  8. #import "GMTools.h"
  9. @interface GMAdManager()
  10. @property(nonatomic,strong)UIView *bannerContainer;
  11. @property(nonatomic,strong)UIView *nativeContainer;
  12. @end
  13. @implementation GMAdManager
  14. /// 打开Banner广告
  15. -(void)openAd:(NSString *)adName{
  16. //以"banner_"开头
  17. if ([adName hasPrefix:@"banner_"]) {
  18. self.bannerContainer.hidden = false;
  19. //横幅广告(Google Mob)
  20. if (self.admobBannerAd.isReady) {
  21. [self.admobBannerAd openWithSuperView:self.bannerContainer];
  22. }else{
  23. //横幅广告(横ironSource)
  24. [self.admobNativeAd loadAd];
  25. [self.isBannerAd openWithSuperView:self.bannerContainer];
  26. }
  27. }
  28. // if ([@"nativeAd" isEqualToString:adName]) {
  29. // [self openNativeAdTop:0 left:0 bottom:0 right:0];
  30. // }
  31. }
  32. /// 打开Banner广告(ironSource平台)
  33. -(void)openIronSourceAd:(NSString *)adName {
  34. }
  35. //打开原生广告
  36. -(void)openNativeAdWithX:(int)x Y:(int)y width:(int)w height:(int)h{
  37. dispatch_async(dispatch_get_main_queue(), ^{
  38. self.nativeContainer.hidden = false;
  39. //CGSize size = [UIScreen mainScreen].bounds.size;
  40. CGRect rect;
  41. //rect = CGRectMake(40, size.height-90-(size.width -80)/5.0*2, size.width - 80, (size.width -80)/5.0*2);
  42. if ([GMTools isIPhoneXSeries]){
  43. rect = CGRectMake(x, y+44, w, h);
  44. }else{
  45. rect = CGRectMake(x, y, w, h);
  46. }
  47. self.nativeContainer.frame = rect;
  48. [self.admobNativeAd openWithSuperView:self.nativeContainer];
  49. });
  50. }
  51. /// 关闭Banner广告
  52. -(void)closeAd:(NSString *)adName{
  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. //1.横幅广告关闭
  55. if ([adName isEqualToString:@"banner"]) {
  56. for (UIView *vi in self.bannerContainer.subviews) {
  57. [vi removeFromSuperview];
  58. }
  59. self.bannerContainer.hidden = true;
  60. [self.admobBannerAd loadAd];
  61. }
  62. //2.原生广告关闭
  63. if ([adName isEqualToString:@"native"]) {
  64. for (UIView *vi in self.nativeContainer.subviews) {
  65. [vi removeFromSuperview];
  66. }
  67. self.nativeContainer.hidden = true;
  68. [self.admobNativeAd loadAd];
  69. }
  70. });
  71. }
  72. /// 打开视频广告1
  73. -(void)openVideoAd:(NSString *)adName callback:(void (^)(BOOL))callback{
  74. [self.rewardVideoAd openVideo:adName callback:callback];
  75. }
  76. /// 打开积分墙广告
  77. -(void)openofferWallAd:(NSString *)adName callback:(void (^)(BOOL))callback {
  78. [self.offerWallVideoAd openOfferWallVideo:adName callback:callback];
  79. }
  80. /// 打开插页广告
  81. -(void)openinterstitialAd:(NSString *)adName callback:(void (^)(BOOL))callback {
  82. [self.interstitialAd openInterstitial:adName callback:callback];
  83. }
  84. #pragma mark - 单利 懒加载
  85. + (instancetype)sharedInstance{return [[super alloc]init];}
  86. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  87. static GMAdManager *instance;
  88. static dispatch_once_t onceToken;
  89. dispatch_once(&onceToken, ^{
  90. instance = [super allocWithZone:zone];
  91. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  92. NSString *APP_KEY = [infos objectForKey:@"ironsource-app-key"];
  93. NSString *userId = [infos objectForKey:@"ironsource-app-userId"];
  94. //NSLog(@"userId=%@",userId);
  95. NSLog(@"APP_KEY=%@",APP_KEY);
  96. [IronSource setUserId:userId];
  97. [IronSource initWithAppKey:APP_KEY];
  98. #ifdef DEBUG
  99. // Debug模式的代码...
  100. [ISIntegrationHelper validateIntegration]; //验证集成结果,根据打印日志判断, 生产环境的的sdk记得注释掉!!!
  101. #else
  102. // Release模式的代码...
  103. #endif
  104. instance.isBannerAd = [[FqIronsourceBanner alloc]init]; //ironsourceBanner
  105. instance.rewardVideoAd = [[FqIronsourceVideo alloc]init];
  106. instance.offerWallVideoAd = [[FqIronsourceOfferWall alloc]init];
  107. instance.admobBannerAd = [[FqAdmobNativeBanner alloc]init];
  108. [instance.admobBannerAd loadAd];
  109. instance.admobNativeAd = [[FqAdmobNativeAd alloc]init];
  110. [instance.admobNativeAd loadAd];
  111. //
  112. instance.interstitialAd = [[FqIronsourceInterstitial alloc]init];
  113. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  114. instance.bannerContainer = [[UIView alloc]init];
  115. instance.bannerContainer.backgroundColor = [UIColor clearColor];
  116. UIViewController *vc = [GMTools getViewControl];
  117. [vc.view addSubview:instance.bannerContainer];
  118. CGSize size = [UIScreen mainScreen].bounds.size;
  119. float height = 60;
  120. if ([GMTools isIPhoneXSeries]) height += 30;
  121. instance.bannerContainer.frame = CGRectMake(0, size.height-height, size.width, height);
  122. instance.bannerContainer.hidden = true;
  123. });
  124. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  125. instance.nativeContainer = [[UIView alloc]init];
  126. instance.nativeContainer.backgroundColor = [UIColor clearColor];
  127. UIViewController *vc = [GMTools getViewControl];
  128. [vc.view addSubview:instance.nativeContainer];
  129. instance.nativeContainer.hidden = true;
  130. });
  131. });
  132. return instance;
  133. }
  134. @end