GMAdManager.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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)openNativeAdTop:(int)top left:(int)left bottom:(int)bottom right:(int)right{
  37. dispatch_async(dispatch_get_main_queue(), ^{
  38. self.nativeContainer.hidden = false;
  39. CGSize size = [UIScreen mainScreen].bounds.size;
  40. CGRect rect;
  41. //默认:left=40,right=40, bottom=40,top=40
  42. if ([GMTools isIPhoneXSeries]){
  43. //rect = CGRectMake(40,size.height- 114-(size.width - 80)/5.0*3.5, size.width - 80, (size.width - 80)/5.0*3.5);
  44. rect = CGRectMake(left,size.height- 114-(size.width-top-bottom)/5.0*3.5, size.width - (left + right), (size.width - top-bottom)/5.0*3.5);
  45. }else{
  46. //rect = CGRectMake(40, size.height-90-(size.width -80)/5.0*2, size.width - 80, (size.width -80)/5.0*2);
  47. rect = CGRectMake(left, size.height-90-(size.width -top-bottom)/5.0*2, size.width - (left+right), (size.width - top-bottom)/5.0*2);
  48. }
  49. self.nativeContainer.frame = rect;
  50. });
  51. [self.admobNativeAd openWithSuperView:self.nativeContainer];
  52. }
  53. /// 关闭Banner广告
  54. -(void)closeAd:(NSString *)adName{
  55. dispatch_async(dispatch_get_main_queue(), ^{
  56. if ([adName isEqualToString:@"banner"]) {
  57. for (UIView *vi in self.bannerContainer.subviews) {
  58. [vi removeFromSuperview];
  59. }
  60. self.bannerContainer.hidden = true;
  61. [self.admobBannerAd loadAd];
  62. }
  63. if ([adName isEqualToString:@"nativeAd"]) {
  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. -(void)openinterstitialAd:(NSString *)adName callback:(void (^)(BOOL))callback {
  81. [self.interstitialAd openInterstitial:adName callback:callback];
  82. }
  83. #pragma mark - 单利 懒加载
  84. + (instancetype)sharedInstance{return [[super alloc]init];}
  85. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  86. static GMAdManager *instance;
  87. static dispatch_once_t onceToken;
  88. dispatch_once(&onceToken, ^{
  89. instance = [super allocWithZone:zone];
  90. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  91. NSString *APP_KEY = [infos objectForKey:@"ironsource-app-key"];
  92. NSString *userId = [infos objectForKey:@"ironsource-app-userId"];
  93. //NSLog(@"userId=%@",userId);
  94. NSLog(@"APP_KEY=%@",APP_KEY);
  95. [IronSource setUserId:userId];
  96. [IronSource initWithAppKey:APP_KEY];
  97. [ISIntegrationHelper validateIntegration];
  98. instance.isBannerAd = [[FqIronsourceBanner alloc]init]; //ironsourceBanner
  99. instance.rewardVideoAd = [[FqIronsourceVideo alloc]init];
  100. instance.offerWallVideoAd = [[FqIronsourceOfferWall alloc]init];
  101. instance.admobBannerAd = [[FqAdmobNativeBanner alloc]init];
  102. [instance.admobBannerAd loadAd];
  103. instance.admobNativeAd = [[FqAdmobNativeAd alloc]init];
  104. [instance.admobNativeAd loadAd];
  105. //
  106. instance.interstitialAd = [[FqIronsourceInterstitial alloc]init];
  107. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  108. instance.bannerContainer = [[UIView alloc]init];
  109. instance.bannerContainer.backgroundColor = [UIColor clearColor];
  110. UIViewController *vc = [GMTools getViewControl];
  111. [vc.view addSubview:instance.bannerContainer];
  112. CGSize size = [UIScreen mainScreen].bounds.size;
  113. float height = 60;
  114. if ([GMTools isIPhoneXSeries]) height += 30;
  115. instance.bannerContainer.frame = CGRectMake(0, size.height-height, size.width, height);
  116. instance.bannerContainer.hidden = true;
  117. });
  118. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  119. instance.nativeContainer = [[UIView alloc]init];
  120. instance.nativeContainer.backgroundColor = [UIColor clearColor];
  121. UIViewController *vc = [GMTools getViewControl];
  122. [vc.view addSubview:instance.nativeContainer];
  123. instance.nativeContainer.hidden = true;
  124. });
  125. });
  126. return instance;
  127. }
  128. @end