FqToPONExpressNativeAd.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // FqToPONExpressNativeAd.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/5/13.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqToPONExpressNativeAd.h"
  9. #import <AnyThinkSDK/AnyThinkSDK.h>
  10. #import <AnyThinkNative/AnyThinkNative.h>
  11. #import "XSNetwork.h"
  12. #import "GMTools.h"
  13. @interface FqToPONExpressNativeAd()<ATNativeADDelegate,ATAdLoadingDelegate>
  14. @property(nonatomic,assign)BOOL isAdReadly;
  15. @property(nonatomic,assign)CGSize adSize;
  16. @property(nonatomic,strong)void(^nativeAdCallback)(BOOL);
  17. @end
  18. @implementation FqToPONExpressNativeAd
  19. -(instancetype)init{
  20. if (self = [super init]) {
  21. self.agentName = @"TopOn"; //平台名称
  22. self.adUnitId = @"native";
  23. self.type = @"native"; //广告类型
  24. self.isAdReadly = NO;
  25. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  26. if ([infos objectForKey:@"topon-native-unitId"]) {
  27. self.adUnitId = [infos objectForKey:@"topon-native-unitId"]; //广告组ID
  28. }
  29. //1.程序启动时,加载广告.
  30. [self loadExpressNativeAdWithPlacementId:self.adUnitId];
  31. }
  32. return self;
  33. }
  34. //广告是否准备好
  35. -(BOOL)isAdReadly {
  36. if ([[ATAdManager sharedManager] nativeAdReadyForPlacementID:self.adUnitId]) {
  37. return YES;
  38. } else {
  39. return NO;
  40. }
  41. return NO;
  42. }
  43. //打开广告
  44. -(void)openNativeAdWithX:(int)x Y:(int)y width:(int)w height:(int)h adId:(NSString *)adName callback:(void (^)(BOOL))callback {
  45. if (self.adUnitId.length == 0) {
  46. NSLog(@"【ToPon广告】,error=原生广告广告位id为空!!!");
  47. [XSNetwork showHudFailure:@"广告位id为空!"];
  48. return;
  49. }
  50. self.adId = adName;
  51. self.nativeAdCallback = callback;
  52. //判断广告是否准备好.
  53. if([self isAdReadly]){
  54. //广告准备好,展示广告.
  55. UIWindow *mainWindow = nil;
  56. if (@available(iOS 13.0, *)) {
  57. mainWindow = [UIApplication sharedApplication].windows.firstObject;
  58. [mainWindow makeKeyWindow];
  59. }else {
  60. mainWindow = [UIApplication sharedApplication].keyWindow;
  61. }
  62. UIViewController *vc = [GMTools getViewControl];
  63. //CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
  64. ATNativeADConfiguration *config = [[ATNativeADConfiguration alloc] init];
  65. config.ADFrame = CGRectMake(x, y, self.adSize.width, self.adSize.height);
  66. config.mediaViewFrame = CGRectMake(0, y+56.0f, self.adSize.width, self.adSize.height - 120.0f);
  67. config.delegate = self;
  68. config.renderingViewClass = [UIView class];
  69. config.rootViewController = mainWindow.rootViewController;
  70. UIView *adView = (UIView*)[[ATAdManager sharedManager] retriveAdViewWithPlacementID:self.adUnitId configuration:config];
  71. adView.tag = 300;
  72. [vc.view addSubview:adView];
  73. }else{
  74. //无广告可展示,加载广告.
  75. //PS:重新加载广告,考虑一直广告加载失败的情况,超过3次加载广告失败,不做任何处理.不然会死循环.
  76. [self loadExpressNativeAdWithPlacementId:self.adUnitId];
  77. [XSNetwork showHudSuccess:@"广告正在加载中"];
  78. }
  79. }
  80. //加载广告资源
  81. -(void)loadExpressNativeAdWithPlacementId:(NSString *)placementId {
  82. //CGFloat w1 = [UIScreen mainScreen].bounds.size.width;
  83. CGSize size = CGSizeMake(600.f, 400.0f);
  84. self.adSize = size;
  85. [[ATAdManager sharedManager] loadADWithPlacementID:self.adUnitId extra:@{kExtraInfoNativeAdSizeKey:[NSValue valueWithCGSize:size]} delegate:self];
  86. }
  87. #pragma mark - ===========ATNativeADDelegate============
  88. -(void) didFinishLoadingADWithPlacementID:(NSString *)placementID {
  89. NSLog(@"Native Ad: didFinishLoadingADWithPlacementID");
  90. }
  91. -(void) didFailToLoadADWithPlacementID:(NSString* )placementID error:(NSError *)error {
  92. NSLog(@"Native Ad: failed to load:%@", error);
  93. self.isAdReadly = NO;
  94. if(self.nativeAdCallback){
  95. self.nativeAdCallback(self.isAdReadly);
  96. }
  97. }
  98. -(void) didClickNativeAdInAdView:(ATNativeADView*)adView placementID:(NSString*)placementID extra:(NSDictionary *)extra{
  99. NSLog(@"FqToPONExpressNativeAd.m:: didClickNativeAdInAdView:placementID:%@ with extra: %@", placementID,extra);
  100. }
  101. -(void) didShowNativeAdInAdView:(ATNativeADView*)adView placementID:(NSString*)placementID extra:(NSDictionary *)extra{
  102. NSLog(@"FqToPONExpressNativeAd.m:: didShowNativeAdInAdView:placementID:%@ with extra: %@", placementID,extra);
  103. self.isAdReadly = YES;
  104. if(self.nativeAdCallback){
  105. self.nativeAdCallback(self.isAdReadly);
  106. }
  107. }
  108. -(void) didTapCloseButtonInAdView:(ATNativeADView*)adView placementID:(NSString*)placementID extra:(NSDictionary *)extra {
  109. NSLog(@"FqToPONExpressNativeAd.m:: didTapCloseButtonInAdView:placementID:%@ extra:%@", placementID, extra);
  110. }
  111. - (void)didDeepLinkOrJumpInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra result:(BOOL)success {
  112. }
  113. - (void)didEndPlayingVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
  114. }
  115. - (void)didEnterFullScreenVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
  116. }
  117. - (void)didExitFullScreenVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
  118. }
  119. - (void)didLoadSuccessDrawWith:(NSArray *)views placementID:(NSString *)placementID extra:(NSDictionary *)extra {
  120. }
  121. - (void)didStartPlayingVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
  122. }
  123. @end