FqAdmobNativeAd.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // FqAdmobNativeAd.m
  3. // QQQ
  4. //
  5. // Created by Mgs on 2020/12/7.
  6. //
  7. #import "FqAdmobNativeAd.h"
  8. #import "XSNetwork.h"
  9. @interface FqAdmobNativeAd()<GADUnifiedNativeAdLoaderDelegate,GADVideoControllerDelegate,GADUnifiedNativeAdDelegate>
  10. @property(nonatomic,strong)GADUnifiedNativeAd *nativeAd;
  11. @property(nonatomic, strong) GADAdLoader *adLoader;
  12. @property(nonatomic, strong) GADUnifiedNativeAdView *nativeAdView;
  13. @property(nonatomic,strong)UIView *containerView;
  14. @end
  15. @implementation FqAdmobNativeAd
  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-native-unitId"];
  24. self.type = @"Native"; //广告类型
  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:@"FqAdmobNativeAd" owner:nil options:nil];
  47. _nativeAdView = (GADUnifiedNativeAdView *)[nibObjects firstObject];
  48. _nativeAdView.backgroundColor = [UIColor whiteColor];
  49. }
  50. return _nativeAdView;
  51. }
  52. -(void)loadAd{
  53. self.isReady = false;
  54. dispatch_async(dispatch_get_main_queue(), ^{
  55. [self.adLoader loadRequest:[GADRequest request]];
  56. });
  57. //数据上报
  58. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  59. }
  60. -(void)openWithSuperView:(UIView *)container{
  61. self.containerView = container;
  62. if (self.nativeAd) {
  63. self.isReady = false;
  64. UIImageView *iconView = (UIImageView *)self.nativeAdView.iconView;
  65. iconView.image = self.nativeAd.icon.image;
  66. UILabel *titleLabel = (UILabel *)self.nativeAdView.headlineView;
  67. titleLabel.textColor = [UIColor blackColor];
  68. titleLabel.text = self.nativeAd.headline;
  69. UILabel *subTitleLabel = (UILabel *)self.nativeAdView.bodyView;
  70. subTitleLabel.textColor = [UIColor grayColor];
  71. subTitleLabel.text = self.nativeAd.body;
  72. UIButton *installBtn = (UIButton *)self.nativeAdView.callToActionView;
  73. // [installBtn setTitle:self.nativeAd.callToAction forState:0];
  74. installBtn.userInteractionEnabled = false;
  75. if (self.nativeAd.images.count>0) {
  76. UIImageView *contentImage = (UIImageView *)self.nativeAdView.imageView;
  77. contentImage.image = [self.nativeAd.images firstObject].image;
  78. }
  79. UIButton *closeBtn = (UIButton *)[self.nativeAdView viewWithTag:9901];
  80. [closeBtn addTarget:self action:@selector(closeAd) forControlEvents:UIControlEventTouchUpInside];
  81. self.nativeAdView.nativeAd = self.nativeAd;
  82. self.nativeAdView.frame = CGRectMake(0, 0, container.bounds.size.width, container.bounds.size.height);
  83. [container addSubview:self.nativeAdView];
  84. //数据上报
  85. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  86. }
  87. }
  88. -(void)closeAd{
  89. self.isReady = false;
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. for (UIView *vi in self.containerView.subviews) {
  92. [vi removeFromSuperview];
  93. }
  94. self.containerView.hidden = true;
  95. });
  96. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  97. }
  98. #pragma mark GADAdLoaderDelegate implementation
  99. - (void)adLoaderDidFinishLoading:(nonnull GADAdLoader *)adLoader{
  100. }
  101. - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error {
  102. self.isReady = false;
  103. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  104. }
  105. #pragma mark GADUnifiedNativeAdLoaderDelegate implementation
  106. - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
  107. self.isReady = true;
  108. self.nativeAd = nativeAd;
  109. }
  110. #pragma mark GADVideoControllerDelegate implementation
  111. - (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController {
  112. }
  113. #pragma mark GADUnifiedNativeAdDelegate
  114. - (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd {
  115. NSLog(@"%s", __PRETTY_FUNCTION__);
  116. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  117. }
  118. - (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd {
  119. NSLog(@"%s", __PRETTY_FUNCTION__);
  120. }
  121. - (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd {
  122. NSLog(@"%s", __PRETTY_FUNCTION__);
  123. }
  124. - (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  125. NSLog(@"%s", __PRETTY_FUNCTION__);
  126. }
  127. - (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  128. NSLog(@"%s", __PRETTY_FUNCTION__);
  129. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  130. }
  131. - (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd {
  132. NSLog(@"%s", __PRETTY_FUNCTION__);
  133. }
  134. @end