// // FqAdmobNativeAd.m // QQQ // // Created by Mgs on 2020/12/7. // #import "FqAdmobNativeAd.h" #import "XSNetwork.h" @interface FqAdmobNativeAd() @property(nonatomic,strong)GADUnifiedNativeAd *nativeAd; @property(nonatomic, strong) GADAdLoader *adLoader; @property(nonatomic, strong) GADUnifiedNativeAdView *nativeAdView; @property(nonatomic,strong)UIView *containerView; @end @implementation FqAdmobNativeAd - (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-native-unitId"]; self.type = @"Native"; //广告类型 self.unitAdId = @""; //广告组ID GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[@"75aaba1b2736b3a627adc11b55bff9d8"]; } 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) { NSBundle *sdkBundle = [NSBundle bundleForClass:[self class]]; // NSArray *nibObjects = [sdkBundle loadNibNamed:@"FqAdmobNativeAd" owner:nil options:nil]; //[[NSBundle mainBundle] loadNibNamed:@"FqAdmobNativeAd" owner:nil options:nil]; //PS: 这行代码在原工程里没问题,在sdk里面获取nib资源报错.不能:[NSBundle mainBundle], 资源不在mainBundle里面. _nativeAdView = (GADUnifiedNativeAdView *)[nibObjects firstObject]; _nativeAdView.backgroundColor = [UIColor whiteColor]; } 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.containerView = 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; UIButton *installBtn = (UIButton *)self.nativeAdView.callToActionView; // [installBtn setTitle:self.nativeAd.callToAction forState:0]; installBtn.userInteractionEnabled = false; if (self.nativeAd.images.count>0) { UIImageView *contentImage = (UIImageView *)self.nativeAdView.imageView; contentImage.image = [self.nativeAd.images firstObject].image; } UIButton *closeBtn = (UIButton *)[self.nativeAdView viewWithTag:9901]; [closeBtn addTarget:self action:@selector(closeAd) forControlEvents:UIControlEventTouchUpInside]; self.nativeAdView.nativeAd = self.nativeAd; self.nativeAdView.frame = CGRectMake(0, 0, container.bounds.size.width, container.bounds.size.height); [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)closeAd{ self.isReady = false; dispatch_async(dispatch_get_main_queue(), ^{ for (UIView *vi in self.containerView.subviews) { [vi removeFromSuperview]; } self.containerView.hidden = true; }); [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg]; } #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