// // FqGDTExpressNativeAd.m // XenonSDK // // Created by fq on 2021/5/8. // Copyright © 2021 SAGESSE. All rights reserved. // #import "FqGDTExpressNativeAd.h" #import "GDTNativeExpressAd.h" #import "GDTNativeExpressAdView.h" #import "GMTools.h" #import #import "XSNetwork.h" @interface FqGDTExpressNativeAd() @property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd; @property (nonatomic, strong) NSMutableArray *expressAdViews; @property(nonatomic,assign)CGRect rect; @property(nonatomic,assign)BOOL isAdReadly; @property(nonatomic,strong)void(^nativeAdCallback)(BOOL); @end @implementation FqGDTExpressNativeAd -(instancetype)init{ if (self = [super init]) { self.agentName = @"YLH"; //平台名称 self.adUnitId = @"Native"; self.type = @"Native"; //广告类型 self.isAdReadly = NO; // NSDictionary *infos = [[NSBundle mainBundle] infoDictionary]; // if ([infos objectForKey:@"ylh-native-unitId"]) { // self.adUnitId = [infos objectForKey:@"ylh-native-unitId"]; //广告组ID // } } return self; } //关闭广告 -(void)closeExpressNatived:(NSString *)adName { if ([adName isEqualToString:self.adUnitId]) { if (self.expressAdViews.count > 0) { GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject]; [childView removeFromSuperview]; [self.expressAdViews removeAllObjects]; self.nativeExpressAd = nil; } }else{ NSLog(@"关闭的:模板2原生广告位id不匹配!"); } } //打开广告 -(void)openExpressNativeAdWithPlacementId:(NSString *)placementId AdName:(NSString *)adName Frame:(CGRect)rect callback:(void (^)(BOOL))callback { self.adId = adName; self.nativeAdCallback = callback; self.adUnitId = placementId; self.rect = rect; if (self.adUnitId.length == 0) { NSLog(@"【优量汇广告】,error=模板2原生广告位id为空!!!"); [XSNetwork showHudFailure:@"广告位id位空!"]; return; } [self InitExpressNativedAdWithPlacementId:self.adUnitId adSize:CGSizeMake(rect.size.width, rect.size.height)]; } //原生广告 -(void)InitExpressNativedAdWithPlacementId:(NSString *)placementId adSize:(CGSize)size{ self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithPlacementId:placementId adSize:size]; self.nativeExpressAd.delegate = self; [self.nativeExpressAd loadAd:3]; //数据上报 [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg]; } #pragma mark - GDTNativeExpressAdDelegete /** * 拉取广告成功的回调 */ - (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd *)nativeExpressAd views:(NSArray<__kindof GDTNativeExpressAdView *> *)views { NSLog(@"%s",__FUNCTION__); self.expressAdViews = [NSMutableArray arrayWithArray:views]; UIWindow *keyWindow = [GMTools getKeyWindow]; if (self.expressAdViews.count) { [self.expressAdViews enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj; expressView.controller = keyWindow.rootViewController; [expressView render]; NSLog(@"eCPM:%ld eCPMLevel:%@", [expressView eCPM], [expressView eCPMLevel]); }]; } UIViewController *v1 = [GMTools getViewControl]; GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject]; childView.frame = self.rect; [v1.view addSubview:childView]; } /** * 拉取广告失败的回调 */ - (void)nativeExpressAdRenderFail:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__FUNCTION__); } /** * 拉取原生模板广告失败 */ - (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error { NSLog(@"%s",__FUNCTION__); NSLog(@"Express Ad Load Fail : %@",error); self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo]; } - (void)nativeExpressAdViewRenderSuccess:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__FUNCTION__); UIViewController *v1 = [GMTools getViewControl]; GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject]; childView.frame = self.rect; [v1.view addSubview:childView]; //数据上报 [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)nativeExpressAdViewClicked:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__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)nativeExpressAdViewClosed:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__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]; //关闭广告. GDTNativeExpressAdView *childView = (GDTNativeExpressAdView *)[self.expressAdViews firstObject]; [childView removeFromSuperview]; [self.expressAdViews removeObject:nativeExpressAdView]; self.nativeExpressAd = nil; NSLog(@"[优量汇]原生广告关闭成功!"); } - (void)nativeExpressAdViewExposure:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__FUNCTION__); } - (void)nativeExpressAdViewWillPresentScreen:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__FUNCTION__); } - (void)nativeExpressAdViewDidPresentScreen:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__FUNCTION__); } - (void)nativeExpressAdViewWillDismissScreen:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__FUNCTION__); } - (void)nativeExpressAdViewDidDismissScreen:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"%s",__FUNCTION__); } /** * 详解:当点击应用下载或者广告调用系统程序打开时调用 */ - (void)nativeExpressAdViewApplicationWillEnterBackground:(GDTNativeExpressAdView *)nativeExpressAdView { NSLog(@"--------%s-------",__FUNCTION__); } @end