ISAdRewarded.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // ISAdRewarded.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2020/12/25.
  6. // Copyright © 2020 SAGESSE. All rights reserved.
  7. //
  8. #import "ISAdRewarded.h"
  9. #import <IronSource/IronSource.h>
  10. #import "XSNetwork.h"
  11. @interface ISAdRewarded()<ISRewardedVideoDelegate>
  12. //是否加载好
  13. @property (nonatomic, assign)BOOL ready;
  14. @end
  15. @implementation ISAdRewarded
  16. - (instancetype)init
  17. {
  18. self = [super init];
  19. if (self) {
  20. self.ready = NO;
  21. self.adId = @""; //广告位名称
  22. self.agentName = @"ironsource"; //平台名称
  23. self.medium = @""; //中介
  24. self.adUnitId = @"Rewarded"; //原平台单元id
  25. self.type = @"Rewarded"; //广告类型
  26. self.unitAdId = @""; //广告组ID
  27. self.errorMsg = @""; //错误信息
  28. //init
  29. [IronSource setRewardedVideoDelegate:self];
  30. }
  31. return self;
  32. }
  33. - (BOOL)isReady {
  34. return self.ready;
  35. }
  36. - (void)play:(NSString *)adId callback:(void (^)(int))back {
  37. [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  38. if (self.ready) {
  39. self.ready = NO;
  40. self.rewardCallback = back;
  41. UIWindow *window = [self currentWindow];
  42. if (window) {
  43. [IronSource showRewardedVideoWithViewController:window.rootViewController];
  44. }
  45. }else{
  46. //视频未准备好
  47. self.rewardCallback(1);
  48. }
  49. }
  50. #pragma mark - ISRewardedVideoDelegate
  51. // This method lets you know whether or not there is a video
  52. // ready to be presented. It is only after this method is invoked
  53. // with 'hasAvailableAds' set to 'YES' that you can should 'showRV'.
  54. - (void)rewardedVideoHasChangedAvailability:(BOOL)available {
  55. NSLog(@"%s", __PRETTY_FUNCTION__);
  56. NSLog(@"ironsource ISAdRewarded rewardedVideoHasChangedAvailability :%d",available);
  57. self.ready = available;
  58. }
  59. // This method gets invoked after the user has been rewarded.
  60. - (void)didReceiveRewardForPlacement:(ISPlacementInfo *)placementInfo {
  61. NSLog(@"%s", __PRETTY_FUNCTION__);
  62. if (self.rewardCallback != nil) {
  63. self.rewardCallback(0);
  64. }
  65. }
  66. // This method gets invoked when there is a problem playing the video.
  67. // If it does happen, check out 'error' for more information and consult
  68. // our knowledge center for help.
  69. - (void)rewardedVideoDidFailToShowWithError:(NSError *)error {
  70. NSLog(@"%s", __PRETTY_FUNCTION__);
  71. [XSNetwork showHudFailure:@"未准备好!"];
  72. //收集错误日志上报服务器.
  73. self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
  74. if (self.rewardCallback != nil) {
  75. self.rewardCallback(1);
  76. }
  77. }
  78. // This method gets invoked when we take control, but before
  79. // the video has started playing.
  80. - (void)rewardedVideoDidOpen {
  81. NSLog(@"%s", __PRETTY_FUNCTION__);
  82. [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  83. }
  84. // This method gets invoked when we return controlback to your hands.
  85. // We chose to notify you about rewards here and not in 'didReceiveRewardForPlacement'.
  86. // This is because reward can occur in the middle of the video.
  87. - (void)rewardedVideoDidClose {
  88. NSLog(@"%s", __PRETTY_FUNCTION__);
  89. [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  90. if (self.rewardCallback != nil) {
  91. self.rewardCallback(2);
  92. }
  93. }
  94. // This method gets invoked when the video has started playing.
  95. - (void)rewardedVideoDidStart {
  96. NSLog(@"%s", __PRETTY_FUNCTION__);
  97. }
  98. // This method gets invoked when the video has stopped playing.
  99. - (void)rewardedVideoDidEnd {
  100. NSLog(@"%s", __PRETTY_FUNCTION__);
  101. }
  102. // This method gets invoked after a video has been clicked
  103. - (void)didClickRewardedVideo:(ISPlacementInfo *)placementInfo {
  104. NSLog(@"%s", __PRETTY_FUNCTION__);
  105. [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
  106. }
  107. @end