FqToPONRewardedVideoAd.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // FqToPONRewardedVideoAd.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/5/12.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "FqToPONRewardedVideoAd.h"
  9. #import <AnyThinkRewardedVideo/AnyThinkRewardedVideo.h>
  10. #import "XSNetwork.h"
  11. #import "GMTools.h"
  12. @interface FqToPONRewardedVideoAd()<ATRewardedVideoDelegate>
  13. @property(nonatomic,assign)BOOL isAdReadly; //广告是否准备好
  14. @property(nonatomic,assign)BOOL isReward; //是否发放奖励
  15. @property(nonatomic,strong)void(^rewardVideoAdCallback)(BOOL,BOOL);
  16. @end
  17. @implementation FqToPONRewardedVideoAd
  18. -(instancetype)init{
  19. if (self = [super init]) {
  20. self.agentName = @"TopOn"; //平台名称
  21. self.adUnitId = @"Rewarded";
  22. self.type = @"Rewarded"; //广告类型
  23. self.isAdReadly = NO;
  24. self.isReward = NO;
  25. NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
  26. if ([infos objectForKey:@"topon-rewarded-unitId"]) {
  27. self.adUnitId = [infos objectForKey:@"topon-rewarded-unitId"]; //广告组ID
  28. }
  29. //1.程序启动时,加载广告.
  30. [self loadRewardedVideoAdWithPlacementId:self.adUnitId];
  31. }
  32. return self;
  33. }
  34. //广告是否准备好
  35. -(BOOL)isAdReadly {
  36. if ([[ATAdManager sharedManager] rewardedVideoReadyForPlacementID:self.adUnitId]) {
  37. return YES;
  38. } else {
  39. return NO;
  40. }
  41. return NO;
  42. }
  43. //打开广告
  44. -(void)openRewardedVideoAd:(NSString *)adName callback:(void (^)(BOOL, 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.rewardVideoAdCallback = callback;
  52. //判断广告是否准备好.
  53. if([self isAdReadly]){
  54. //广告准备好,展示广告.
  55. UIViewController *vc = [GMTools getViewControl];
  56. [[ATAdManager sharedManager] showRewardedVideoWithPlacementID:self.adUnitId inViewController:vc delegate:self];
  57. }else{
  58. //无广告可展示,加载广告.
  59. //PS:重新加载广告,考虑一直广告加载失败的情况,超过3次加载广告失败,不做任何处理.不然会死循环.
  60. [self loadRewardedVideoAdWithPlacementId:self.adUnitId];
  61. [XSNetwork showHudSuccess:@"广告正在加载中"];
  62. }
  63. }
  64. //加载广告资源
  65. -(void)loadRewardedVideoAdWithPlacementId:(NSString *)placementId {
  66. //注:在加载facebook广告的时候一定要传入userid,以获取激励视频回调. extra:@{kATAdLoadingExtraUserIDKey:@"test_user_id"}
  67. [[ATAdManager sharedManager] loadADWithPlacementID:self.adUnitId extra:nil delegate:self];
  68. }
  69. #pragma mark - =============ATRewardedVideoDelegate==============
  70. -(void) didFinishLoadingADWithPlacementID:(NSString *)placementID {
  71. NSLog(@"RV: didFinishLoadingADWithPlacementID");
  72. self.isAdReadly = YES;
  73. if (self.rewardVideoAdCallback) {
  74. self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
  75. }
  76. }
  77. -(void) didFailToLoadADWithPlacementID:(NSString* )placementID error:(NSError *)error {
  78. NSLog(@"RV: failed to load:%@", error);
  79. self.isAdReadly = NO;
  80. self.isReward = NO;
  81. if (self.rewardVideoAdCallback) {
  82. self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
  83. }
  84. }
  85. -(void) rewardedVideoDidRewardSuccessForPlacemenID:(NSString *)placementID extra:(NSDictionary *)extra{
  86. NSLog(@"FqToPONRewardedVideoAd.m::rewardedVideoDidRewardSuccessForPlacemenID:%@ extra:%@",placementID,extra);
  87. }
  88. -(void) rewardedVideoDidStartPlayingForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra {
  89. NSLog(@"FqToPONRewardedVideoAd.m::rewardedVideoDidStartPlayingForPlacementID:%@ extra:%@", placementID, extra);
  90. }
  91. -(void) rewardedVideoDidEndPlayingForPlacementID:(NSString*)placementID extra:(NSDictionary *)extra {
  92. NSLog(@"FqToPONRewardedVideoAd.m::rewardedVideoDidEndPlayingForPlacementID:%@ extra:%@", placementID, extra);
  93. }
  94. -(void) rewardedVideoDidFailToPlayForPlacementID:(NSString*)placementID error:(NSError*)error extra:(NSDictionary *)extra {
  95. NSLog(@"FqToPONRewardedVideoAd.m::rewardedVideoDidFailToPlayForPlacementID:%@ error:%@ extra:%@", placementID, error, extra);
  96. }
  97. -(void) rewardedVideoDidCloseForPlacementID:(NSString*)placementID rewarded:(BOOL)rewarded extra:(NSDictionary *)extra {
  98. NSLog(@"FqToPONRewardedVideoAd.m::rewardedVideoDidCloseForPlacementID:%@, rewarded:%@ extra:%@", placementID, rewarded ? @"yes" : @"no", extra);
  99. self.isAdReadly = YES;
  100. self.isReward = rewarded;
  101. if (self.rewardVideoAdCallback) {
  102. self.rewardVideoAdCallback(YES, self.isReward);
  103. }
  104. }
  105. -(void) rewardedVideoDidClickForPlacementID:(NSString*)placementID extra:(NSDictionary *)extra {
  106. NSLog(@"FqToPONRewardedVideoAd.m::rewardedVideoDidClickForPlacementID:%@ extra:%@", placementID, extra);
  107. }
  108. - (void)rewardedVideoDidDeepLinkOrJumpForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra result:(BOOL)success {
  109. }
  110. @end