123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- //
- // FqToPONExpressNativeAd.m
- // XenonSDK
- //
- // Created by fq on 2021/5/13.
- // Copyright © 2021 SAGESSE. All rights reserved.
- //
- #import "FqToPONExpressNativeAd.h"
- #import <AnyThinkSDK/AnyThinkSDK.h>
- #import <AnyThinkNative/AnyThinkNative.h>
- #import "XSNetwork.h"
- #import "GMTools.h"
- @interface FqToPONExpressNativeAd()<ATNativeADDelegate,ATAdLoadingDelegate>
- @property(nonatomic,assign)BOOL isAdReadly;
- @property(nonatomic,assign)CGSize adSize;
- @property(nonatomic,strong)void(^nativeAdCallback)(BOOL);
- @end
- @implementation FqToPONExpressNativeAd
- -(instancetype)init{
- if (self = [super init]) {
- self.agentName = @"TopOn"; //平台名称
- self.adUnitId = @"native";
- self.type = @"native"; //广告类型
- self.isAdReadly = NO;
- NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
- if ([infos objectForKey:@"topon-native-unitId"]) {
- self.adUnitId = [infos objectForKey:@"topon-native-unitId"]; //广告组ID
- }
-
- //1.程序启动时,加载广告.
- [self loadExpressNativeAdWithPlacementId:self.adUnitId];
- }
- return self;
- }
- //广告是否准备好
- -(BOOL)isAdReadly {
- if ([[ATAdManager sharedManager] nativeAdReadyForPlacementID:self.adUnitId]) {
- return YES;
- } else {
- return NO;
- }
- return NO;
- }
- //打开广告
- -(void)openNativeAdWithX:(int)x Y:(int)y width:(int)w height:(int)h adId:(NSString *)adName callback:(void (^)(BOOL))callback {
- if (self.adUnitId.length == 0) {
- NSLog(@"【ToPon广告】,error=原生广告广告位id为空!!!");
- [XSNetwork showHudFailure:@"广告位id为空!"];
- return;
- }
- self.adId = adName;
- self.nativeAdCallback = callback;
-
- //判断广告是否准备好.
- if([self isAdReadly]){
- //广告准备好,展示广告.
-
- UIWindow *mainWindow = nil;
- if (@available(iOS 13.0, *)) {
- mainWindow = [UIApplication sharedApplication].windows.firstObject;
- [mainWindow makeKeyWindow];
- }else {
- mainWindow = [UIApplication sharedApplication].keyWindow;
- }
-
- UIViewController *vc = [GMTools getViewControl];
- //CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
- ATNativeADConfiguration *config = [[ATNativeADConfiguration alloc] init];
- config.ADFrame = CGRectMake(x, y, self.adSize.width, self.adSize.height);
- config.mediaViewFrame = CGRectMake(0, y+56.0f, self.adSize.width, self.adSize.height - 120.0f);
- config.delegate = self;
- config.renderingViewClass = [UIView class];
- config.rootViewController = mainWindow.rootViewController;
- UIView *adView = (UIView*)[[ATAdManager sharedManager] retriveAdViewWithPlacementID:self.adUnitId configuration:config];
- adView.tag = 300;
- [vc.view addSubview:adView];
-
- }else{
- //无广告可展示,加载广告.
- //PS:重新加载广告,考虑一直广告加载失败的情况,超过3次加载广告失败,不做任何处理.不然会死循环.
- [self loadExpressNativeAdWithPlacementId:self.adUnitId];
- [XSNetwork showHudSuccess:@"广告正在加载中"];
- }
- }
- //加载广告资源
- -(void)loadExpressNativeAdWithPlacementId:(NSString *)placementId {
-
- //CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
- CGSize size = CGSizeMake(600.f, 400.0f);
- self.adSize = size;
- [[ATAdManager sharedManager] loadADWithPlacementID:self.adUnitId extra:@{kExtraInfoNativeAdSizeKey:[NSValue valueWithCGSize:size]} delegate:self];
-
- }
- #pragma mark - ===========ATNativeADDelegate============
- -(void) didFinishLoadingADWithPlacementID:(NSString *)placementID {
- NSLog(@"Native Ad: didFinishLoadingADWithPlacementID");
-
- }
- -(void) didFailToLoadADWithPlacementID:(NSString* )placementID error:(NSError *)error {
- NSLog(@"Native Ad: failed to load:%@", error);
-
- self.isAdReadly = NO;
- if(self.nativeAdCallback){
- self.nativeAdCallback(self.isAdReadly);
- }
- }
- -(void) didClickNativeAdInAdView:(ATNativeADView*)adView placementID:(NSString*)placementID extra:(NSDictionary *)extra{
- NSLog(@"FqToPONExpressNativeAd.m:: didClickNativeAdInAdView:placementID:%@ with extra: %@", placementID,extra);
- }
- -(void) didShowNativeAdInAdView:(ATNativeADView*)adView placementID:(NSString*)placementID extra:(NSDictionary *)extra{
- NSLog(@"FqToPONExpressNativeAd.m:: didShowNativeAdInAdView:placementID:%@ with extra: %@", placementID,extra);
- self.isAdReadly = YES;
- if(self.nativeAdCallback){
- self.nativeAdCallback(self.isAdReadly);
- }
- }
- -(void) didTapCloseButtonInAdView:(ATNativeADView*)adView placementID:(NSString*)placementID extra:(NSDictionary *)extra {
-
- NSLog(@"FqToPONExpressNativeAd.m:: didTapCloseButtonInAdView:placementID:%@ extra:%@", placementID, extra);
- }
- - (void)didDeepLinkOrJumpInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra result:(BOOL)success {
-
- }
- - (void)didEndPlayingVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
-
- }
- - (void)didEnterFullScreenVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
-
- }
- - (void)didExitFullScreenVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
-
- }
- - (void)didLoadSuccessDrawWith:(NSArray *)views placementID:(NSString *)placementID extra:(NSDictionary *)extra {
-
- }
- - (void)didStartPlayingVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
-
- }
- @end
|