// // AdmobNative.m // XenonSDK // // Created by fq on 2020/12/24. // Copyright © 2020 SAGESSE. All rights reserved. // #import "AdmobNative.h" #import @interface AdmobNative() @end @implementation AdmobNative ///刷新广告 -(void)refreshAd { UIWindow *window = [[UIApplication sharedApplication].delegate window]; GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; videoOptions.startMuted = NO; //不开始视频 self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.AdUnit rootViewController:window.rootViewController adTypes:@[kGADAdLoaderAdTypeUnifiedNative] options:@[videoOptions]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GADRequest request]]; } /// - (void)setAdView:(TestNativeAdView *)view { // Remove previous ad view. [self.nativeAdView removeFromSuperview]; self.nativeAdView = view; } ///是否准备好 -(BOOL)isReady { return self.ready; } //init - (instancetype)init { self = [super init]; if (self) { self.AdUnit = @"ca-app-pub-3940256099942544/3986624511"; self.isShow = NO; self.ready = NO; self.adId = @""; //广告位名称 self.agentName = @"ironsource"; //平台名称 self.medium = @""; //中介 self.adUnitId = @"Native"; //原平台单元id self.type = @"Native"; //广告类型 self.unitAdId = @""; //广告组ID self.errorMsg = @""; //错误信息 //GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[@"d2f3edea5fa209e7aafe5a1f92053835"]; //获取bundle NSArray *nibObjects = [NSBundle.mainBundle loadNibNamed:@"TestNativeAdView" owner:nil options:nil]; [self setAdView:[nibObjects firstObject]]; //adLoader init UIWindow *window = [[UIApplication sharedApplication].delegate window]; GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; videoOptions.startMuted = NO; //不开始视频 self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.AdUnit rootViewController:window.rootViewController adTypes:@[kGADAdLoaderAdTypeUnifiedNative] options:@[videoOptions]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GADRequest request]]; } return self; } //显示原生广告 - (void)showNativeWithID:(NSString *)adId X:(int)x Y:(int)y W:(int)w H:(int)h { if (self.isShow) { return; } self.isShow = YES; self.nativeAdPlaceholder = [[UIView alloc]initWithFrame:CGRectMake(x, y, w, h)]; self.nativeAdView.frame = CGRectMake(0, 0, self.nativeAdPlaceholder.frame.size.width, self.nativeAdPlaceholder.frame.size.height); [self.nativeAdPlaceholder addSubview:self.nativeAdView]; UIWindow *window = [[UIApplication sharedApplication].delegate window]; [window addSubview:self.nativeAdPlaceholder]; } //关闭原生广告 -(void)close { self.isShow = NO; if (self.nativeAdPlaceholder != nil) { [self.nativeAdPlaceholder removeFromSuperview]; self.nativeAdPlaceholder = nil; } } #pragma mark GADAdLoaderDelegate implementation - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error { NSLog(@"%@ failed with error: %@", adLoader, error); //收集错误日志上报服务器. 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.ready = YES; TestNativeAdView *nativeAdView = self.nativeAdView; // Deactivate the height constraint that was set when the previous video ad loaded. self.heightConstraint.active = NO; // Set ourselves as the ad delegate to be notified of native ad events. nativeAd.delegate = self; //1. headlineView // Populate the native ad view with the native ad assets. // The headline and mediaContent are guaranteed to be present in every native ad. ((UILabel *)nativeAdView.headlineView).text = nativeAd.headline; //2. iconView ((UIImageView *)nativeAdView.iconView).image = nativeAd.icon.image; nativeAdView.iconView.hidden = nativeAd.icon ? NO : YES; //3. advertiserView ((UILabel *)nativeAdView.advertiserView).text = nativeAd.advertiser; nativeAdView.advertiserView.hidden = nativeAd.advertiser ? NO : YES; //4. bodyView // ((UILabel *)nativeAdView.bodyView).text = nativeAd.body; // nativeAdView.bodyView.hidden = nativeAd.body ? NO : YES; nativeAdView.mediaView.mediaContent = nativeAd.mediaContent; // This app uses a fixed width for the GADMediaView and changes its height // to match the aspect ratio of the media content it displays. if (nativeAd.mediaContent.aspectRatio > 0) { self.heightConstraint = [NSLayoutConstraint constraintWithItem:nativeAdView.mediaView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nativeAdView.mediaView attribute:NSLayoutAttributeWidth multiplier:(1 / nativeAd.mediaContent.aspectRatio) constant:0]; self.heightConstraint.active = YES; } NSLog(@"mediaView.frame:%@",NSStringFromCGRect(nativeAdView.mediaView.frame)); if (nativeAd.mediaContent.hasVideoContent) { // By acting as the delegate to the GADVideoController, this ViewController // receives messages about events in the video lifecycle. nativeAd.mediaContent.videoController.delegate = self; } // Associate the native ad view with the native ad object. This is // required to make the ad clickable. // Note: this should always be done after populating the ad views. nativeAdView.nativeAd = nativeAd; [((UIButton *)nativeAdView.callToActionView) setTitle:nativeAd.callToAction forState:UIControlStateNormal]; nativeAdView.callToActionView.hidden = nativeAd.callToAction ? NO : YES; /** ((UILabel *)nativeAdView.storeView).text = nativeAd.store; nativeAdView.storeView.hidden = nativeAd.store ? NO : YES; // These assets are not guaranteed to be present. Check that they are before // showing or hiding them. ((UILabel *)nativeAdView.priceView).text = nativeAd.price; nativeAdView.priceView.hidden = nativeAd.price ? NO : YES; */ // In order for the SDK to process touch events properly, user interaction // should be disabled. nativeAdView.callToActionView.userInteractionEnabled = NO; [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg]; } #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