FqAdmobNativeAd.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[@"75aaba1b2736b3a627adc11b55bff9d8"];
  27. }
  28. return self;
  29. }
  30. -(GADAdLoader *)adLoader{
  31. if (!_adLoader) {
  32. //NSString *nativeAdId = @"ca-app-pub-3940256099942544/3986624511";
  33. UIViewController *vc = [GMTools getViewControl];
  34. GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
  35. _adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.adUnitId
  36. rootViewController:vc
  37. adTypes:@[ kGADAdLoaderAdTypeUnifiedNative ]
  38. options:@[ videoOptions ]];
  39. _adLoader.delegate = self;
  40. self.nativeAd = nil;
  41. }
  42. return _adLoader;
  43. }
  44. -(GADUnifiedNativeAdView *)nativeAdView{
  45. if (!_nativeAdView) {
  46. NSBundle *sdkBundle = [NSBundle bundleForClass:[self class]];
  47. //
  48. NSArray *nibObjects = [sdkBundle loadNibNamed:@"FqAdmobNativeAd" owner:nil options:nil];
  49. //[[NSBundle mainBundle] loadNibNamed:@"FqAdmobNativeAd" owner:nil options:nil];
  50. //PS: 这行代码在原工程里没问题,在sdk里面获取nib资源报错.不能:[NSBundle mainBundle], 资源不在mainBundle里面.
  51. _nativeAdView = (GADUnifiedNativeAdView *)[nibObjects firstObject];
  52. _nativeAdView.backgroundColor = [UIColor whiteColor];
  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.containerView = 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. UIButton *installBtn = (UIButton *)self.nativeAdView.callToActionView;
  77. // [installBtn setTitle:self.nativeAd.callToAction forState:0];
  78. installBtn.userInteractionEnabled = false;
  79. if (self.nativeAd.images.count>0) {
  80. UIImageView *contentImage = (UIImageView *)self.nativeAdView.imageView;
  81. contentImage.image = [self.nativeAd.images firstObject].image;
  82. }
  83. UIButton *closeBtn = (UIButton *)[self.nativeAdView viewWithTag:9901];
  84. [closeBtn addTarget:self action:@selector(closeAd) forControlEvents:UIControlEventTouchUpInside];
  85. self.nativeAdView.nativeAd = self.nativeAd;
  86. self.nativeAdView.frame = CGRectMake(0, 0, container.bounds.size.width, container.bounds.size.height);
  87. [container addSubview:self.nativeAdView];
  88. //数据上报
  89. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  90. }
  91. }
  92. -(void)closeAd{
  93. self.isReady = false;
  94. dispatch_async(dispatch_get_main_queue(), ^{
  95. for (UIView *vi in self.containerView.subviews) {
  96. [vi removeFromSuperview];
  97. }
  98. self.containerView.hidden = true;
  99. });
  100. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  101. }
  102. #pragma mark GADAdLoaderDelegate implementation
  103. - (void)adLoaderDidFinishLoading:(nonnull GADAdLoader *)adLoader{
  104. }
  105. - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error {
  106. self.isReady = false;
  107. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  108. }
  109. #pragma mark GADUnifiedNativeAdLoaderDelegate implementation
  110. - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
  111. self.isReady = true;
  112. self.nativeAd = nativeAd;
  113. }
  114. #pragma mark GADVideoControllerDelegate implementation
  115. - (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController {
  116. }
  117. #pragma mark GADUnifiedNativeAdDelegate
  118. - (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd {
  119. NSLog(@"%s", __PRETTY_FUNCTION__);
  120. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  121. }
  122. - (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd {
  123. NSLog(@"%s", __PRETTY_FUNCTION__);
  124. }
  125. - (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd {
  126. NSLog(@"%s", __PRETTY_FUNCTION__);
  127. }
  128. - (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  129. NSLog(@"%s", __PRETTY_FUNCTION__);
  130. }
  131. - (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  132. NSLog(@"%s", __PRETTY_FUNCTION__);
  133. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  134. }
  135. - (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd {
  136. NSLog(@"%s", __PRETTY_FUNCTION__);
  137. }
  138. @end