BondSDKOpenMediationAdapter.mm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // BondSDKOpenMediationAdapter.mm
  3. // Pods
  4. //
  5. // Created by daisy on 2020/10/23.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "BondSDKOpenMediationAdapter.h"
  9. #include "cocos/bindings/jswrapper/SeApi.h"
  10. @implementation BondSDKOpenMediationAdapter
  11. static BondSDKOpenMediationAdapter *mInstace = nil;
  12. + (instancetype)getInstance {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. mInstace = [[super allocWithZone:NULL] init];
  16. });
  17. return mInstace;
  18. }
  19. + (void) initSDK:(NSString *) appKey {
  20. [OpenMediation initWithAppKey:appKey];
  21. [self getInstance];
  22. [mInstace callback:@"onAdCallback" funName:@"createAd" param:@""];
  23. //初始化激励视频监听
  24. [[OMRewardedVideo sharedInstance] addDelegate:mInstace];
  25. }
  26. - (void)callback:(NSString *) callbackName funName:(NSString *) name param:(NSString *) param{
  27. // Class cl = NSClassFromString(@"AppController");
  28. // SEL selector1 = NSSelectorFromString(@"callJs:");
  29. NSString* params = [NSString stringWithFormat:@"window.%@(\"%@\",\"%@\")", callbackName, name, param];
  30. // [cl performSelector:selector1 withObject:(id)params];
  31. const char * s =[params UTF8String];
  32. se::ScriptEngine::getInstance()->evalString(s);
  33. }
  34. + (void) createBannerAd:(NSString *) adUnitId {
  35. mInstace.mBanner = [[OMBanner alloc] initWithBannerType:OMBannerTypeSmart placementID:adUnitId];
  36. [mInstace.mBanner addLayoutAttribute:OMBannerLayoutAttributeHorizontally constant:0];
  37. [mInstace.mBanner addLayoutAttribute:OMBannerLayoutAttributeVertically constant:0];
  38. mInstace.mBanner.delegate = mInstace;
  39. [mInstace.mBanner loadAndShow];
  40. }
  41. + (void) showBannerAd{
  42. [mInstace.mBanner removeFromSuperview];
  43. [mInstace addAdView: mInstace.mBanner atPos:kPosBottom];
  44. }
  45. + (void) hideBannerAd{
  46. [mInstace.mBanner removeFromSuperview];
  47. }
  48. - (void) addAdView:(UIView*) view atPos:(AdsPosEnum) pos
  49. {
  50. UIViewController* controller = [self getCurrentRootViewController];
  51. if (nil == controller) {
  52. NSLog(@"Can't get the UIViewController object");
  53. return;
  54. }
  55. CGSize rootSize = controller.view.frame.size;
  56. CGSize viewSize = view.frame.size;
  57. CGPoint viewOrigin;
  58. // if (UIInterfaceOrientationIsLandscape(controller.interfaceOrientation)){
  59. // CGFloat temp = rootSize.width;
  60. // rootSize.width = rootSize.height;
  61. // rootSize.height = temp;
  62. // }
  63. switch (pos) {
  64. case kPosTop:
  65. viewOrigin.x = (rootSize.width - viewSize.width) / 2;
  66. viewOrigin.y = 0.0f;
  67. break;
  68. case kPosTopLeft:
  69. viewOrigin.x = 0.0f;
  70. viewOrigin.y = 0.0f;
  71. break;
  72. case kPosTopRight:
  73. viewOrigin.x = rootSize.width - viewSize.width;
  74. viewOrigin.y = 0.0f;
  75. break;
  76. case kPosBottom:
  77. viewOrigin.x = (rootSize.width - viewSize.width) / 2;
  78. viewOrigin.y = rootSize.height - viewSize.height;
  79. break;
  80. case kPosBottomLeft:
  81. viewOrigin.x = 0.0f;
  82. viewOrigin.y = rootSize.height - viewSize.height;
  83. break;
  84. case kPosBottomRight:
  85. viewOrigin.x = rootSize.width - viewSize.width;
  86. viewOrigin.y = rootSize.height - viewSize.height;
  87. break;
  88. case kPosCenter:
  89. default:
  90. viewOrigin.x = (rootSize.width - viewSize.width) / 2;
  91. viewOrigin.y = (rootSize.height - viewSize.height) / 2;
  92. break;
  93. }
  94. CGRect rect = CGRectMake(viewOrigin.x, viewOrigin.y, viewSize.width, viewSize.height);
  95. view.frame = rect;
  96. [controller.view addSubview:view];
  97. }
  98. - (UIViewController *)getCurrentRootViewController {
  99. UIViewController *result = nil;
  100. // Try to find the root view controller programmically
  101. // Find the top window (that is not an alert view or other window)
  102. UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
  103. if (topWindow.windowLevel != UIWindowLevelNormal)
  104. {
  105. NSArray *windows = [[UIApplication sharedApplication] windows];
  106. for(topWindow in windows)
  107. {
  108. if (topWindow.windowLevel == UIWindowLevelNormal)
  109. break;
  110. }
  111. }
  112. UIView *rootView = [[topWindow subviews] objectAtIndex:0];
  113. id nextResponder = [rootView nextResponder];
  114. if ([nextResponder isKindOfClass:[UIViewController class]])
  115. result = nextResponder;
  116. else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
  117. result = topWindow.rootViewController;
  118. else
  119. NSAssert(NO, @"Could not find a root view controller.");
  120. return result;
  121. }
  122. /// Invoked when the banner ad is available.
  123. - (void)omBannerDidLoad:(OMBanner *)banner {
  124. NSLog(@"BannerAd Did Load");
  125. [mInstace callback:@"onBannerAdCallback" funName:@"adViewDidLoadAd" param:@""];
  126. }
  127. /// Sent after an AdTimingBanner fails to load the ad.
  128. - (void) omBannerDidFailToLoad:(OMBanner *)banner withError:(NSError *)error {
  129. [mInstace callback:@"onBannerAdCallback" funName:@"didFailToLoadAd" param:@""];
  130. }
  131. + (void) createInterstitialAd:(NSString *) adUnitId {
  132. [[OMInterstitial sharedInstance] addDelegate:mInstace];
  133. }
  134. + (void) showInterstitialAd:(NSString *) adUnitId {
  135. if ([[OMInterstitial sharedInstance] isReady]) {
  136. [[OMInterstitial sharedInstance] showWithViewController:[mInstace getCurrentRootViewController] scene:@""];
  137. }
  138. }
  139. /// Invoked when interstitial video is available.
  140. - (void)omInterstitialChangedAvailability:(BOOL)available{
  141. if(available){
  142. [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialDidLoadAd" param:@""];
  143. } else {
  144. [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialDidFailToLoadAd" param:@""];
  145. }
  146. }
  147. /// Sent immediately when a interstitial video is opened.
  148. - (void)omInterstitialDidOpen:(OMScene *)scene{
  149. NSLog(@"InterstitialAd is Open");
  150. }
  151. /// Sent immediately when a interstitial video starts to play.
  152. - (void)omInterstitialDidShow:(OMScene *)scene{
  153. [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialAdSuccess" param:@""];
  154. }
  155. /// Sent after a interstitial video has been clicked.
  156. - (void)omInterstitialDidClick:(OMScene *)scene{
  157. NSLog(@"InterstitialAd Did Click");
  158. }
  159. /// Sent after a interstitial video has been closed.
  160. - (void)omInterstitialDidClose:(OMScene *)scene{
  161. NSLog(@"InterstitialAd Did Close");
  162. }
  163. /// Sent after a interstitial video has failed to play.
  164. - (void)omInterstitialDidFailToShow:(OMScene *)scene withError:(NSError *)error{
  165. NSLog(@"InterstitialAd failed to play");
  166. [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialDidFailToLoadAd" param:@""];
  167. }
  168. + (void) createRewardedVideoAd:(NSString *) adUnitId {
  169. NSLog(@"bondsdkadapter createRewardedVideoAd");
  170. // [[OMRewardedVideo sharedInstance] addDelegate:mInstace];
  171. }
  172. + (void) showRewardedVideoAd:(NSString *) adUnitId {
  173. NSLog(@"bondsdkadapter showRewardedVideoAd");
  174. if ([[OMRewardedVideo sharedInstance] isReady]) {
  175. [[OMRewardedVideo sharedInstance] showWithViewController:[mInstace getCurrentRootViewController] scene:@""];
  176. }
  177. }
  178. /// Invoked when rewarded video is available.
  179. - (void)omRewardedVideoChangedAvailability:(BOOL)available{
  180. if(available){
  181. [mInstace callback:@"onVideoAdCallback" funName:@"videoAdDidLoad" param:@""];
  182. } else {
  183. [mInstace callback:@"onVideoAdCallback" funName:@"videoAdDidFailToLoad" param:@""];
  184. }
  185. }
  186. /// Sent after a rewarded video has been closed.
  187. - (void)omRewardedVideoDidClose:(OMScene *)scene{
  188. NSLog(@"VideoAd Did Close");
  189. [mInstace callback:@"onVideoAdCallback" funName:@"videoAdSuccess" param:@""];
  190. }
  191. /// Sent after a user has been granted a reward.
  192. - (void)omRewardedVideoDidReceiveReward:(OMScene *)scene{
  193. [mInstace callback:@"onVideoAdCallback" funName:@"videoAdShouldReward" param:@""];
  194. }
  195. /// Sent after a rewarded video has failed to play.
  196. - (void)omRewardedVideoDidFailToShow:(OMScene *)scene withError:(NSError *)error{
  197. [mInstace callback:@"onVideoAdCallback" funName:@"videoAdFail" param:@""];
  198. }
  199. @end