AdmobNative.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // AdmobNative.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2020/12/24.
  6. // Copyright © 2020 SAGESSE. All rights reserved.
  7. //
  8. #import "AdmobNative.h"
  9. #import <XenonSDK.h>
  10. @interface AdmobNative() <GADUnifiedNativeAdLoaderDelegate,
  11. GADVideoControllerDelegate,
  12. GADUnifiedNativeAdDelegate>
  13. @end
  14. @implementation AdmobNative
  15. ///刷新广告
  16. -(void)refreshAd {
  17. UIWindow *window = [[UIApplication sharedApplication].delegate window];
  18. GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
  19. videoOptions.startMuted = NO; //不开始视频
  20. self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.AdUnit
  21. rootViewController:window.rootViewController
  22. adTypes:@[kGADAdLoaderAdTypeUnifiedNative]
  23. options:@[videoOptions]];
  24. self.adLoader.delegate = self;
  25. [self.adLoader loadRequest:[GADRequest request]];
  26. }
  27. ///
  28. - (void)setAdView:(TestNativeAdView *)view {
  29. // Remove previous ad view.
  30. [self.nativeAdView removeFromSuperview];
  31. self.nativeAdView = view;
  32. }
  33. ///是否准备好
  34. -(BOOL)isReady {
  35. return self.ready;
  36. }
  37. //init
  38. - (instancetype)init
  39. {
  40. self = [super init];
  41. if (self) {
  42. self.AdUnit = @"ca-app-pub-3940256099942544/3986624511";
  43. self.isShow = NO;
  44. self.ready = NO;
  45. self.adId = @""; //广告位名称
  46. self.agentName = @"ironsource"; //平台名称
  47. self.medium = @""; //中介
  48. self.adUnitId = @"Native"; //原平台单元id
  49. self.type = @"Native"; //广告类型
  50. self.unitAdId = @""; //广告组ID
  51. self.errorMsg = @""; //错误信息
  52. //GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[@"d2f3edea5fa209e7aafe5a1f92053835"];
  53. //获取bundle
  54. NSArray *nibObjects =
  55. [NSBundle.mainBundle loadNibNamed:@"TestNativeAdView" owner:nil options:nil];
  56. [self setAdView:[nibObjects firstObject]];
  57. //adLoader init
  58. UIWindow *window = [[UIApplication sharedApplication].delegate window];
  59. GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
  60. videoOptions.startMuted = NO; //不开始视频
  61. self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.AdUnit
  62. rootViewController:window.rootViewController
  63. adTypes:@[kGADAdLoaderAdTypeUnifiedNative]
  64. options:@[videoOptions]];
  65. self.adLoader.delegate = self;
  66. [self.adLoader loadRequest:[GADRequest request]];
  67. }
  68. return self;
  69. }
  70. //显示原生广告
  71. - (void)showNativeWithID:(NSString *)adId X:(int)x Y:(int)y W:(int)w H:(int)h {
  72. if (self.isShow) {
  73. return;
  74. }
  75. self.isShow = YES;
  76. self.nativeAdPlaceholder = [[UIView alloc]initWithFrame:CGRectMake(x, y, w, h)];
  77. self.nativeAdView.frame = CGRectMake(0, 0, self.nativeAdPlaceholder.frame.size.width, self.nativeAdPlaceholder.frame.size.height);
  78. [self.nativeAdPlaceholder addSubview:self.nativeAdView];
  79. UIWindow *window = [[UIApplication sharedApplication].delegate window];
  80. [window addSubview:self.nativeAdPlaceholder];
  81. }
  82. //关闭原生广告
  83. -(void)close {
  84. self.isShow = NO;
  85. if (self.nativeAdPlaceholder != nil) {
  86. [self.nativeAdPlaceholder removeFromSuperview];
  87. self.nativeAdPlaceholder = nil;
  88. }
  89. }
  90. #pragma mark GADAdLoaderDelegate implementation
  91. - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error {
  92. NSLog(@"%@ failed with error: %@", adLoader, error);
  93. //收集错误日志上报服务器.
  94. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  95. }
  96. #pragma mark GADUnifiedNativeAdLoaderDelegate implementation
  97. - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
  98. self.ready = YES;
  99. TestNativeAdView *nativeAdView = self.nativeAdView;
  100. // Deactivate the height constraint that was set when the previous video ad loaded.
  101. self.heightConstraint.active = NO;
  102. // Set ourselves as the ad delegate to be notified of native ad events.
  103. nativeAd.delegate = self;
  104. //1. headlineView
  105. // Populate the native ad view with the native ad assets.
  106. // The headline and mediaContent are guaranteed to be present in every native ad.
  107. ((UILabel *)nativeAdView.headlineView).text = nativeAd.headline;
  108. //2. iconView
  109. ((UIImageView *)nativeAdView.iconView).image = nativeAd.icon.image;
  110. nativeAdView.iconView.hidden = nativeAd.icon ? NO : YES;
  111. //3. advertiserView
  112. ((UILabel *)nativeAdView.advertiserView).text = nativeAd.advertiser;
  113. nativeAdView.advertiserView.hidden = nativeAd.advertiser ? NO : YES;
  114. //4. bodyView
  115. // ((UILabel *)nativeAdView.bodyView).text = nativeAd.body;
  116. // nativeAdView.bodyView.hidden = nativeAd.body ? NO : YES;
  117. nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;
  118. // This app uses a fixed width for the GADMediaView and changes its height
  119. // to match the aspect ratio of the media content it displays.
  120. if (nativeAd.mediaContent.aspectRatio > 0) {
  121. self.heightConstraint =
  122. [NSLayoutConstraint constraintWithItem:nativeAdView.mediaView
  123. attribute:NSLayoutAttributeHeight
  124. relatedBy:NSLayoutRelationEqual
  125. toItem:nativeAdView.mediaView
  126. attribute:NSLayoutAttributeWidth
  127. multiplier:(1 / nativeAd.mediaContent.aspectRatio)
  128. constant:0];
  129. self.heightConstraint.active = YES;
  130. }
  131. NSLog(@"mediaView.frame:%@",NSStringFromCGRect(nativeAdView.mediaView.frame));
  132. if (nativeAd.mediaContent.hasVideoContent) {
  133. // By acting as the delegate to the GADVideoController, this ViewController
  134. // receives messages about events in the video lifecycle.
  135. nativeAd.mediaContent.videoController.delegate = self;
  136. }
  137. // Associate the native ad view with the native ad object. This is
  138. // required to make the ad clickable.
  139. // Note: this should always be done after populating the ad views.
  140. nativeAdView.nativeAd = nativeAd;
  141. [((UIButton *)nativeAdView.callToActionView) setTitle:nativeAd.callToAction
  142. forState:UIControlStateNormal];
  143. nativeAdView.callToActionView.hidden = nativeAd.callToAction ? NO : YES;
  144. /**
  145. ((UILabel *)nativeAdView.storeView).text = nativeAd.store;
  146. nativeAdView.storeView.hidden = nativeAd.store ? NO : YES;
  147. // These assets are not guaranteed to be present. Check that they are before
  148. // showing or hiding them.
  149. ((UILabel *)nativeAdView.priceView).text = nativeAd.price;
  150. nativeAdView.priceView.hidden = nativeAd.price ? NO : YES;
  151. */
  152. // In order for the SDK to process touch events properly, user interaction
  153. // should be disabled.
  154. nativeAdView.callToActionView.userInteractionEnabled = NO;
  155. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  156. }
  157. #pragma mark GADVideoControllerDelegate implementation
  158. - (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController {
  159. }
  160. #pragma mark GADUnifiedNativeAdDelegate
  161. - (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd {
  162. NSLog(@"%s", __PRETTY_FUNCTION__);
  163. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  164. }
  165. - (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd {
  166. NSLog(@"%s", __PRETTY_FUNCTION__);
  167. }
  168. - (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd {
  169. NSLog(@"%s", __PRETTY_FUNCTION__);
  170. }
  171. - (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  172. NSLog(@"%s", __PRETTY_FUNCTION__);
  173. }
  174. - (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  175. NSLog(@"%s", __PRETTY_FUNCTION__);
  176. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  177. }
  178. - (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd {
  179. NSLog(@"%s", __PRETTY_FUNCTION__);
  180. }
  181. @end