// // FqAdmobNativeBanner.m // QQQ // // Created by Mgs on 2020/12/7. // #import "FqAdmobNativeBanner.h" #import "XSNetwork.h" @interface FqAdmobNativeBanner() @property(nonatomic,strong)UIView *bannerContainer; @property(nonatomic,strong)GADUnifiedNativeAd *nativeAd; @property(nonatomic, strong) GADAdLoader *adLoader; @property(nonatomic, strong) GADUnifiedNativeAdView *nativeAdView; @end @implementation FqAdmobNativeBanner - (instancetype)init { self = [super init]; if (self) { self.agentName = @"admob"; //平台名称 //@"ca-app-pub-3940256099942544/3986624511" NSDictionary *infos = [[NSBundle mainBundle] infoDictionary]; self.adUnitId = [infos objectForKey:@"mob-banner-unitId"]; self.type = @"Nativebanner"; //广告类型 self.unitAdId = @""; //广告组ID } return self; } -(GADAdLoader *)adLoader{ if (!_adLoader) { //NSString *nativeAdId = @"ca-app-pub-3940256099942544/3986624511"; UIViewController *vc = [GMTools getViewControl]; GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; _adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.adUnitId rootViewController:vc adTypes:@[ kGADAdLoaderAdTypeUnifiedNative ] options:@[ videoOptions ]]; _adLoader.delegate = self; self.nativeAd = nil; } return _adLoader; } -(GADUnifiedNativeAdView *)nativeAdView{ if (!_nativeAdView) { //NSArray *nibObjects = //[[NSBundle mainBundle] loadNibNamed:@"FqAdmobNativeBanner" owner:nil options:nil]; NSBundle *sdkBundle = [NSBundle bundleForClass:[self class]]; NSArray *nibObjects = [sdkBundle loadNibNamed:@"FqAdmobNativeBanner" owner:nil options:nil]; _nativeAdView = (GADUnifiedNativeAdView *)[nibObjects firstObject]; _nativeAdView.backgroundColor = [UIColor whiteColor]; CGSize size = [UIScreen mainScreen].bounds.size; _nativeAdView.frame = CGRectMake(0, 0, size.width, size.width/320.0*50); UIButton *closeBtn = [_nativeAdView viewWithTag:8871]; [closeBtn addTarget:self action:@selector(closeBtnClicked) forControlEvents:UIControlEventTouchUpInside]; } return _nativeAdView; } -(void)loadAd{ self.isReady = false; dispatch_async(dispatch_get_main_queue(), ^{ [self.adLoader loadRequest:[GADRequest request]]; }); //数据上报 [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg]; } -(void)openWithSuperView:(UIView *)container{ self.bannerContainer = container; if (self.nativeAd) { self.isReady = false; UIImageView *iconView = (UIImageView *)self.nativeAdView.iconView; iconView.image = self.nativeAd.icon.image; UILabel *titleLabel = (UILabel *)self.nativeAdView.headlineView; titleLabel.textColor = [UIColor blackColor]; titleLabel.text = self.nativeAd.headline; UILabel *subTitleLabel = (UILabel *)self.nativeAdView.bodyView; subTitleLabel.textColor = [UIColor grayColor]; subTitleLabel.text = self.nativeAd.body; UILabel *lab = [self.nativeAdView viewWithTag:4437]; lab.text = self.nativeAd.callToAction; self.nativeAdView.nativeAd = self.nativeAd; [container addSubview:self.nativeAdView]; //数据上报 [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg]; } } -(void)closeBtnClicked{ dispatch_async(dispatch_get_main_queue(), ^{ for (UIView *view in self.bannerContainer.subviews) { [view removeFromSuperview]; } self.bannerContainer.hidden = true; [self loadAd]; }); } #pragma mark GADAdLoaderDelegate implementation - (void)adLoaderDidFinishLoading:(nonnull GADAdLoader *)adLoader{ } - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error { self.isReady = false; self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo]; } #pragma mark GADUnifiedNativeAdLoaderDelegate implementation - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd { self.isReady = true; self.nativeAd = nativeAd; } #pragma mark GADVideoControllerDelegate implementation - (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController { } #pragma mark GADUnifiedNativeAdDelegate - (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd { NSLog(@"%s", __PRETTY_FUNCTION__); [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg]; } - (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd { NSLog(@"%s", __PRETTY_FUNCTION__); } - (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd { NSLog(@"%s", __PRETTY_FUNCTION__); } - (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd { NSLog(@"%s", __PRETTY_FUNCTION__); } - (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd { NSLog(@"%s", __PRETTY_FUNCTION__); [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg]; } - (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd { NSLog(@"%s", __PRETTY_FUNCTION__); } @end