You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

129 lines
3.8 KiB

  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. #import "GADURewardBasedVideoAd.h"
  3. #import <CoreGraphics/CoreGraphics.h>
  4. #import <UIKit/UIKit.h>
  5. #import "GADUPluginUtil.h"
  6. #import "UnityAppController.h"
  7. #import "UnityInterface.h"
  8. @interface GADURewardBasedVideoAd () <GADRewardBasedVideoAdDelegate>
  9. @end
  10. @implementation GADURewardBasedVideoAd
  11. + (UIViewController *)unityGLViewController {
  12. UnityAppController *applicationDelegate = [UIApplication sharedApplication].delegate;
  13. return applicationDelegate.rootViewController;
  14. }
  15. - (instancetype)initWithRewardBasedVideoClientReference:
  16. (GADUTypeRewardBasedVideoAdClientRef *)rewardBasedVideoAdClient {
  17. self = [super init];
  18. if (self) {
  19. _rewardBasedVideoAdClient = rewardBasedVideoAdClient;
  20. _rewardBasedVideo = [GADRewardBasedVideoAd sharedInstance];
  21. _rewardBasedVideo.delegate = self;
  22. }
  23. return self;
  24. }
  25. - (void)dealloc {
  26. _rewardBasedVideo.delegate = nil;
  27. }
  28. - (void)loadRequest:(GADRequest *)request withAdUnitID:(NSString *)adUnitID {
  29. [self.rewardBasedVideo loadRequest:request withAdUnitID:adUnitID];
  30. }
  31. - (BOOL)isReady {
  32. return [self.rewardBasedVideo isReady];
  33. }
  34. - (void)show {
  35. if ([self.rewardBasedVideo isReady]) {
  36. UIViewController *unityController = [GADURewardBasedVideoAd unityGLViewController];
  37. [self.rewardBasedVideo presentFromRootViewController:unityController];
  38. } else {
  39. NSLog(@"GoogleMobileAdsPlugin: Reward based video ad is not ready to be shown.");
  40. }
  41. }
  42. - (void)setUserId:(NSString *)userId {
  43. self.rewardBasedVideo.userIdentifier = userId;
  44. }
  45. - (NSString *)mediationAdapterClassName {
  46. return [self.rewardBasedVideo adNetworkClassName];
  47. }
  48. #pragma mark GADRewardBasedVideoAdDelegate implementation
  49. - (void)rewardBasedVideoAdDidReceiveAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  50. if (self.adReceivedCallback) {
  51. self.adReceivedCallback(self.rewardBasedVideoAdClient);
  52. }
  53. }
  54. - (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd
  55. didFailToLoadWithError:(NSError *)error {
  56. if (self.adFailedCallback) {
  57. NSString *errorMsg = [NSString
  58. stringWithFormat:@"Failed to receive ad with error: %@", [error localizedFailureReason]];
  59. self.adFailedCallback(self.rewardBasedVideoAdClient,
  60. [errorMsg cStringUsingEncoding:NSUTF8StringEncoding]);
  61. }
  62. }
  63. - (void)rewardBasedVideoAdDidOpen:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  64. if ([GADUPluginUtil pauseOnBackground]) {
  65. UnityPause(YES);
  66. }
  67. if (self.didOpenCallback) {
  68. self.didOpenCallback(self.rewardBasedVideoAdClient);
  69. }
  70. }
  71. - (void)rewardBasedVideoAdDidStartPlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  72. if (self.didStartPlayingCallback) {
  73. self.didStartPlayingCallback(self.rewardBasedVideoAdClient);
  74. }
  75. }
  76. - (void)rewardBasedVideoAdDidClose:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  77. if (UnityIsPaused()) {
  78. UnityPause(NO);
  79. }
  80. if (self.didCloseCallback) {
  81. self.didCloseCallback(self.rewardBasedVideoAdClient);
  82. }
  83. }
  84. - (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd
  85. didRewardUserWithReward:(GADAdReward *)reward {
  86. if (self.didRewardCallback) {
  87. // Integer value used for didRewardCallback callback to maintain consistency with Android
  88. // implementation.
  89. self.didRewardCallback(self.rewardBasedVideoAdClient,
  90. [reward.type cStringUsingEncoding:NSUTF8StringEncoding],
  91. reward.amount.doubleValue);
  92. }
  93. }
  94. - (void)rewardBasedVideoAdWillLeaveApplication:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  95. if (self.willLeaveCallback) {
  96. self.willLeaveCallback(self.rewardBasedVideoAdClient);
  97. }
  98. }
  99. - (void)rewardBasedVideoAdDidCompletePlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd {
  100. if (self.didCompleteCallback) {
  101. self.didCompleteCallback(self.rewardBasedVideoAdClient);
  102. }
  103. }
  104. @end