FqAdmobNativeBanner.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. NSBundle *sdkBundle = [NSBundle bundleForClass:[self class]];
  48. NSArray *nibObjects = [sdkBundle loadNibNamed:@"FqAdmobNativeBanner" owner:nil options:nil];
  49. _nativeAdView = (GADUnifiedNativeAdView *)[nibObjects firstObject];
  50. _nativeAdView.backgroundColor = [UIColor whiteColor];
  51. CGSize size = [UIScreen mainScreen].bounds.size;
  52. _nativeAdView.frame = CGRectMake(0, 0, size.width, size.width/320.0*50);
  53. UIButton *closeBtn = [_nativeAdView viewWithTag:8871];
  54. [closeBtn addTarget:self action:@selector(closeBtnClicked) forControlEvents:UIControlEventTouchUpInside];
  55. }
  56. return _nativeAdView;
  57. }
  58. -(void)loadAd{
  59. self.isReady = false;
  60. dispatch_async(dispatch_get_main_queue(), ^{
  61. [self.adLoader loadRequest:[GADRequest request]];
  62. });
  63. //数据上报
  64. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  65. }
  66. -(void)openWithSuperView:(UIView *)container{
  67. self.bannerContainer = container;
  68. if (self.nativeAd) {
  69. self.isReady = false;
  70. UIImageView *iconView = (UIImageView *)self.nativeAdView.iconView;
  71. iconView.image = self.nativeAd.icon.image;
  72. UILabel *titleLabel = (UILabel *)self.nativeAdView.headlineView;
  73. titleLabel.textColor = [UIColor blackColor];
  74. titleLabel.text = self.nativeAd.headline;
  75. UILabel *subTitleLabel = (UILabel *)self.nativeAdView.bodyView;
  76. subTitleLabel.textColor = [UIColor grayColor];
  77. subTitleLabel.text = self.nativeAd.body;
  78. UILabel *lab = [self.nativeAdView viewWithTag:4437];
  79. lab.text = self.nativeAd.callToAction;
  80. self.nativeAdView.nativeAd = self.nativeAd;
  81. [container addSubview:self.nativeAdView];
  82. //数据上报
  83. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  84. }
  85. }
  86. -(void)closeBtnClicked{
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. for (UIView *view in self.bannerContainer.subviews) {
  89. [view removeFromSuperview];
  90. }
  91. self.bannerContainer.hidden = true;
  92. [self loadAd];
  93. });
  94. }
  95. #pragma mark GADAdLoaderDelegate implementation
  96. - (void)adLoaderDidFinishLoading:(nonnull GADAdLoader *)adLoader{
  97. }
  98. - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error {
  99. self.isReady = false;
  100. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  101. }
  102. #pragma mark GADUnifiedNativeAdLoaderDelegate implementation
  103. - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
  104. self.isReady = true;
  105. self.nativeAd = nativeAd;
  106. }
  107. #pragma mark GADVideoControllerDelegate implementation
  108. - (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController {
  109. }
  110. #pragma mark GADUnifiedNativeAdDelegate
  111. - (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd {
  112. NSLog(@"%s", __PRETTY_FUNCTION__);
  113. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  114. }
  115. - (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd {
  116. NSLog(@"%s", __PRETTY_FUNCTION__);
  117. }
  118. - (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd {
  119. NSLog(@"%s", __PRETTY_FUNCTION__);
  120. }
  121. - (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  122. NSLog(@"%s", __PRETTY_FUNCTION__);
  123. }
  124. - (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  125. NSLog(@"%s", __PRETTY_FUNCTION__);
  126. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  127. }
  128. - (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd {
  129. NSLog(@"%s", __PRETTY_FUNCTION__);
  130. }
  131. @end