// // BondSDKOpenMediationAdapter.mm // Pods // // Created by daisy on 2020/10/23. // #import #import "BondSDKOpenMediationAdapter.h" #include "cocos/bindings/jswrapper/SeApi.h" @implementation BondSDKOpenMediationAdapter static BondSDKOpenMediationAdapter *mInstace = nil; + (instancetype)getInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ mInstace = [[super allocWithZone:NULL] init]; }); return mInstace; } + (void) initSDK:(NSString *) appKey { [OpenMediation initWithAppKey:appKey]; [self getInstance]; [mInstace callback:@"onAdCallback" funName:@"createAd" param:@""]; //初始化激励视频监听 [[OMRewardedVideo sharedInstance] addDelegate:mInstace]; } - (void)callback:(NSString *) callbackName funName:(NSString *) name param:(NSString *) param{ // Class cl = NSClassFromString(@"AppController"); // SEL selector1 = NSSelectorFromString(@"callJs:"); NSString* params = [NSString stringWithFormat:@"window.%@(\"%@\",\"%@\")", callbackName, name, param]; // [cl performSelector:selector1 withObject:(id)params]; const char * s =[params UTF8String]; se::ScriptEngine::getInstance()->evalString(s); } + (void) createBannerAd:(NSString *) adUnitId { mInstace.mBanner = [[OMBanner alloc] initWithBannerType:OMBannerTypeSmart placementID:adUnitId]; [mInstace.mBanner addLayoutAttribute:OMBannerLayoutAttributeHorizontally constant:0]; [mInstace.mBanner addLayoutAttribute:OMBannerLayoutAttributeVertically constant:0]; mInstace.mBanner.delegate = mInstace; [mInstace.mBanner loadAndShow]; } + (void) showBannerAd{ [mInstace.mBanner removeFromSuperview]; [mInstace addAdView: mInstace.mBanner atPos:kPosBottom]; } + (void) hideBannerAd{ [mInstace.mBanner removeFromSuperview]; } - (void) addAdView:(UIView*) view atPos:(AdsPosEnum) pos { UIViewController* controller = [self getCurrentRootViewController]; if (nil == controller) { NSLog(@"Can't get the UIViewController object"); return; } CGSize rootSize = controller.view.frame.size; CGSize viewSize = view.frame.size; CGPoint viewOrigin; // if (UIInterfaceOrientationIsLandscape(controller.interfaceOrientation)){ // CGFloat temp = rootSize.width; // rootSize.width = rootSize.height; // rootSize.height = temp; // } switch (pos) { case kPosTop: viewOrigin.x = (rootSize.width - viewSize.width) / 2; viewOrigin.y = 0.0f; break; case kPosTopLeft: viewOrigin.x = 0.0f; viewOrigin.y = 0.0f; break; case kPosTopRight: viewOrigin.x = rootSize.width - viewSize.width; viewOrigin.y = 0.0f; break; case kPosBottom: viewOrigin.x = (rootSize.width - viewSize.width) / 2; viewOrigin.y = rootSize.height - viewSize.height; break; case kPosBottomLeft: viewOrigin.x = 0.0f; viewOrigin.y = rootSize.height - viewSize.height; break; case kPosBottomRight: viewOrigin.x = rootSize.width - viewSize.width; viewOrigin.y = rootSize.height - viewSize.height; break; case kPosCenter: default: viewOrigin.x = (rootSize.width - viewSize.width) / 2; viewOrigin.y = (rootSize.height - viewSize.height) / 2; break; } CGRect rect = CGRectMake(viewOrigin.x, viewOrigin.y, viewSize.width, viewSize.height); view.frame = rect; [controller.view addSubview:view]; } - (UIViewController *)getCurrentRootViewController { UIViewController *result = nil; // Try to find the root view controller programmically // Find the top window (that is not an alert view or other window) UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow]; if (topWindow.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplication sharedApplication] windows]; for(topWindow in windows) { if (topWindow.windowLevel == UIWindowLevelNormal) break; } } UIView *rootView = [[topWindow subviews] objectAtIndex:0]; id nextResponder = [rootView nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) result = nextResponder; else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil) result = topWindow.rootViewController; else NSAssert(NO, @"Could not find a root view controller."); return result; } /// Invoked when the banner ad is available. - (void)omBannerDidLoad:(OMBanner *)banner { NSLog(@"BannerAd Did Load"); [mInstace callback:@"onBannerAdCallback" funName:@"adViewDidLoadAd" param:@""]; } /// Sent after an AdTimingBanner fails to load the ad. - (void) omBannerDidFailToLoad:(OMBanner *)banner withError:(NSError *)error { [mInstace callback:@"onBannerAdCallback" funName:@"didFailToLoadAd" param:@""]; } + (void) createInterstitialAd:(NSString *) adUnitId { [[OMInterstitial sharedInstance] addDelegate:mInstace]; } + (void) showInterstitialAd:(NSString *) adUnitId { if ([[OMInterstitial sharedInstance] isReady]) { [[OMInterstitial sharedInstance] showWithViewController:[mInstace getCurrentRootViewController] scene:@""]; } } /// Invoked when interstitial video is available. - (void)omInterstitialChangedAvailability:(BOOL)available{ if(available){ [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialDidLoadAd" param:@""]; } else { [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialDidFailToLoadAd" param:@""]; } } /// Sent immediately when a interstitial video is opened. - (void)omInterstitialDidOpen:(OMScene *)scene{ NSLog(@"InterstitialAd is Open"); } /// Sent immediately when a interstitial video starts to play. - (void)omInterstitialDidShow:(OMScene *)scene{ [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialAdSuccess" param:@""]; } /// Sent after a interstitial video has been clicked. - (void)omInterstitialDidClick:(OMScene *)scene{ NSLog(@"InterstitialAd Did Click"); } /// Sent after a interstitial video has been closed. - (void)omInterstitialDidClose:(OMScene *)scene{ NSLog(@"InterstitialAd Did Close"); } /// Sent after a interstitial video has failed to play. - (void)omInterstitialDidFailToShow:(OMScene *)scene withError:(NSError *)error{ NSLog(@"InterstitialAd failed to play"); [mInstace callback:@"onInterstitialAdCallback" funName:@"interstitialDidFailToLoadAd" param:@""]; } + (void) createRewardedVideoAd:(NSString *) adUnitId { NSLog(@"bondsdkadapter createRewardedVideoAd"); // [[OMRewardedVideo sharedInstance] addDelegate:mInstace]; } + (void) showRewardedVideoAd:(NSString *) adUnitId { NSLog(@"bondsdkadapter showRewardedVideoAd"); if ([[OMRewardedVideo sharedInstance] isReady]) { [[OMRewardedVideo sharedInstance] showWithViewController:[mInstace getCurrentRootViewController] scene:@""]; } } /// Invoked when rewarded video is available. - (void)omRewardedVideoChangedAvailability:(BOOL)available{ if(available){ [mInstace callback:@"onVideoAdCallback" funName:@"videoAdDidLoad" param:@""]; } else { [mInstace callback:@"onVideoAdCallback" funName:@"videoAdDidFailToLoad" param:@""]; } } /// Sent after a rewarded video has been closed. - (void)omRewardedVideoDidClose:(OMScene *)scene{ NSLog(@"VideoAd Did Close"); [mInstace callback:@"onVideoAdCallback" funName:@"videoAdSuccess" param:@""]; } /// Sent after a user has been granted a reward. - (void)omRewardedVideoDidReceiveReward:(OMScene *)scene{ [mInstace callback:@"onVideoAdCallback" funName:@"videoAdShouldReward" param:@""]; } /// Sent after a rewarded video has failed to play. - (void)omRewardedVideoDidFailToShow:(OMScene *)scene withError:(NSError *)error{ [mInstace callback:@"onVideoAdCallback" funName:@"videoAdFail" param:@""]; } @end