FqAdmobNativeBanner.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // FqAdmobNativeBanner.m
  3. // QQQ
  4. //
  5. // Created by Mgs on 2020/12/7.
  6. //
  7. #import "FqAdmobNativeBanner.h"
  8. #import "XSNetwork.h"
  9. @interface FqAdmobNativeBanner()<GADUnifiedNativeAdLoaderDelegate,GADVideoControllerDelegate,GADUnifiedNativeAdDelegate>
  10. @property(nonatomic,strong)UIView *bannerContainer;
  11. @property(nonatomic,strong)GADUnifiedNativeAd *nativeAd;
  12. @property(nonatomic, strong) GADAdLoader *adLoader;
  13. @property(nonatomic, strong) GADUnifiedNativeAdView *nativeAdView;
  14. @end
  15. @implementation FqAdmobNativeBanner
  16. - (instancetype)init
  17. {
  18. self = [super init];
  19. if (self) {
  20. self.agentName = @"admob"; //平台名称
  21. //@"ca-app-pub-3940256099942544/3986624511"
  22. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  23. self.adUnitId = [infos objectForKey:@"mob-banner-unitId"];
  24. self.type = @"Nativebanner"; //广告类型
  25. self.unitAdId = @""; //广告组ID
  26. }
  27. return self;
  28. }
  29. -(GADAdLoader *)adLoader{
  30. if (!_adLoader) {
  31. //NSString *nativeAdId = @"ca-app-pub-3940256099942544/3986624511";
  32. UIViewController *vc = [GMTools getViewControl];
  33. GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
  34. _adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.adUnitId
  35. rootViewController:vc
  36. adTypes:@[ kGADAdLoaderAdTypeUnifiedNative ]
  37. options:@[ videoOptions ]];
  38. _adLoader.delegate = self;
  39. self.nativeAd = nil;
  40. }
  41. return _adLoader;
  42. }
  43. -(GADUnifiedNativeAdView *)nativeAdView{
  44. if (!_nativeAdView) {
  45. NSArray *nibObjects =
  46. [[NSBundle mainBundle] loadNibNamed:@"FqAdmobNativeBanner" owner:nil options:nil];
  47. _nativeAdView = (GADUnifiedNativeAdView *)[nibObjects firstObject];
  48. _nativeAdView.backgroundColor = [UIColor whiteColor];
  49. CGSize size = [UIScreen mainScreen].bounds.size;
  50. _nativeAdView.frame = CGRectMake(0, 0, size.width, size.width/320.0*50);
  51. UIButton *closeBtn = [_nativeAdView viewWithTag:8871];
  52. [closeBtn addTarget:self action:@selector(closeBtnClicked) forControlEvents:UIControlEventTouchUpInside];
  53. }
  54. return _nativeAdView;
  55. }
  56. -(void)loadAd{
  57. self.isReady = false;
  58. dispatch_async(dispatch_get_main_queue(), ^{
  59. [self.adLoader loadRequest:[GADRequest request]];
  60. });
  61. //数据上报
  62. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  63. }
  64. -(void)openWithSuperView:(UIView *)container{
  65. self.bannerContainer = container;
  66. if (self.nativeAd) {
  67. self.isReady = false;
  68. UIImageView *iconView = (UIImageView *)self.nativeAdView.iconView;
  69. iconView.image = self.nativeAd.icon.image;
  70. UILabel *titleLabel = (UILabel *)self.nativeAdView.headlineView;
  71. titleLabel.textColor = [UIColor blackColor];
  72. titleLabel.text = self.nativeAd.headline;
  73. UILabel *subTitleLabel = (UILabel *)self.nativeAdView.bodyView;
  74. subTitleLabel.textColor = [UIColor grayColor];
  75. subTitleLabel.text = self.nativeAd.body;
  76. UILabel *lab = [self.nativeAdView viewWithTag:4437];
  77. lab.text = self.nativeAd.callToAction;
  78. self.nativeAdView.nativeAd = self.nativeAd;
  79. [container addSubview:self.nativeAdView];
  80. //数据上报
  81. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  82. }
  83. }
  84. -(void)closeBtnClicked{
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. for (UIView *view in self.bannerContainer.subviews) {
  87. [view removeFromSuperview];
  88. }
  89. self.bannerContainer.hidden = true;
  90. [self loadAd];
  91. });
  92. }
  93. #pragma mark GADAdLoaderDelegate implementation
  94. - (void)adLoaderDidFinishLoading:(nonnull GADAdLoader *)adLoader{
  95. }
  96. - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error {
  97. self.isReady = false;
  98. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  99. }
  100. #pragma mark GADUnifiedNativeAdLoaderDelegate implementation
  101. - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
  102. self.isReady = true;
  103. self.nativeAd = nativeAd;
  104. }
  105. #pragma mark GADVideoControllerDelegate implementation
  106. - (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController {
  107. }
  108. #pragma mark GADUnifiedNativeAdDelegate
  109. - (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd {
  110. NSLog(@"%s", __PRETTY_FUNCTION__);
  111. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  112. }
  113. - (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd {
  114. NSLog(@"%s", __PRETTY_FUNCTION__);
  115. }
  116. - (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd {
  117. NSLog(@"%s", __PRETTY_FUNCTION__);
  118. }
  119. - (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  120. NSLog(@"%s", __PRETTY_FUNCTION__);
  121. }
  122. - (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  123. NSLog(@"%s", __PRETTY_FUNCTION__);
  124. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  125. }
  126. - (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd {
  127. NSLog(@"%s", __PRETTY_FUNCTION__);
  128. }
  129. @end