Skip to content

Instantly share code, notes, and snippets.

@pSapien
Created August 2, 2023 05:33
Show Gist options
  • Save pSapien/0a3ac907c12a8c1269812893dd0d84cb to your computer and use it in GitHub Desktop.
Save pSapien/0a3ac907c12a8c1269812893dd0d84cb to your computer and use it in GitHub Desktop.
A bridged module for React Native and uses the Yodo1Mas SDK to handle ads.
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <Yodo1MasCore/Yodo1Mas.h>
NSString * const EVENT_NAME = @"yodo1ad";
NSString * const INIT_SUCCESS = @"init-success";
NSString * const INIT_FAILURE = @"init-failure";
NSString * const REWARD_LOAD_SUCCESS = @"reward-load-success";
NSString * const REWARD_LOAD_FAILURE = @"reward-load-failure";
NSString * const REWARD_OPENED_SUCCESS = @"reward-open-success";
NSString * const REWARD_OPENED_FAILURE = @"reward-open-failure";
NSString * const REWARD_CLOSED = @"reward-closed";
NSString * const REWARD_EARNED = @"reward-earned";
NSString * const INTERSTITIAL_LOAD_SUCCESS = @"interstitial-load-success";
NSString * const INTERSTITIAL_LOAD_FAILURE = @"interstitial-load-failure";
NSString * const INTERSTITIAL_OPENED_SUCCESS = @"interstitial-open-success";
NSString * const INTERSTITIAL_OPENED_FAILURE = @"interstitial-open-failure";
NSString * const INTERSTITIAL_CLOSED = @"interstitial-closed";
@interface Yodo1MASAds : RCTEventEmitter <RCTBridgeModule,
Yodo1MasRewardDelegate,
Yodo1MasInterstitialDelegate>
@end
@implementation Yodo1MASAds {
BOOL initialized;
}
RCT_EXPORT_METHOD(initSdk: (NSString *) appKey) {
dispatch_async(dispatch_get_main_queue(), ^{
Yodo1MasAdBuildConfig *config = [Yodo1MasAdBuildConfig instance];
[[Yodo1Mas sharedInstance] setAdBuildConfig:config];
[[Yodo1Mas sharedInstance] setIsCOPPAAgeRestricted:NO];
[[Yodo1Mas sharedInstance] setIsGDPRUserConsent:YES];
[[Yodo1Mas sharedInstance] setIsCCPADoNotSell:NO];
[[Yodo1Mas sharedInstance] initMasWithAppKey:appKey
successful:^{
[self sendEvent:INIT_SUCCESS error:nil];
self->initialized = YES;
}
fail:^(NSError *_Nonnull error) {
[self sendEvent:INIT_FAILURE error:error];
}];
[Yodo1MasInterstitialAd sharedInstance].adDelegate = self;
[Yodo1MasRewardAd sharedInstance].adDelegate = self;
});
}
- (NSArray<NSString *> *)supportedEvents {
return @[EVENT_NAME];
}
RCT_EXPORT_METHOD(isInitialized:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
resolve([NSNumber numberWithBool:initialized]);
}
RCT_EXPORT_METHOD(showDebugger) {
dispatch_async(dispatch_get_main_queue(), ^{
[[Yodo1Mas sharedInstance] showDebugger];
});
}
RCT_EXPORT_METHOD(loadRewardedAds) {
dispatch_async(dispatch_get_main_queue(), ^{
[[Yodo1MasRewardAd sharedInstance] loadAd];
});
}
RCT_EXPORT_METHOD(showRewardedAds) {
dispatch_async(dispatch_get_main_queue(), ^{
[[Yodo1MasRewardAd sharedInstance] showAd];
});
}
RCT_EXPORT_METHOD(loadInterstitialAds) {
dispatch_async(dispatch_get_main_queue(), ^{
[[Yodo1MasInterstitialAd sharedInstance] loadAd];
});
}
RCT_EXPORT_METHOD(showInterstitialAds) {
dispatch_async(dispatch_get_main_queue(), ^{
[[Yodo1MasInterstitialAd sharedInstance] showAd];
});
}
- (void)sendEvent:(NSString *)type error:(NSError *)error {
NSDictionary *body = @{
@"type": type,
@"error": (error ? error.description : [NSNull null])
};
[self sendEventWithName:EVENT_NAME body:body];
}
#pragma mark - Yodo1MasInterstitialDelegate
- (void)onInterstitialAdLoaded:(Yodo1MasInterstitialAd *)ad {
[self sendEvent:INTERSTITIAL_LOAD_SUCCESS error:nil];
}
- (void)onInterstitialAdFailedToLoad:(Yodo1MasInterstitialAd *)ad withError:(Yodo1MasError *)error {
[self sendEvent:INTERSTITIAL_LOAD_FAILURE error:error];
}
- (void)onInterstitialAdOpened:(Yodo1MasInterstitialAd *)ad {
[self sendEvent:INTERSTITIAL_OPENED_SUCCESS error:nil];
}
- (void)onInterstitialAdFailedToOpen:(Yodo1MasInterstitialAd *)ad withError:(Yodo1MasError *)error {
[self sendEvent:INTERSTITIAL_OPENED_FAILURE error:error];
}
- (void)onInterstitialAdClosed:(Yodo1MasInterstitialAd *)ad {
[self sendEvent:INTERSTITIAL_CLOSED error:nil];
}
#pragma mark - Yodo1MasRewardDelegate
- (void)onRewardAdLoaded:(Yodo1MasRewardAd *)ad {
[self sendEvent:REWARD_LOAD_SUCCESS error:nil];
}
- (void)onRewardAdFailedToLoad:(Yodo1MasRewardAd *)ad withError:(Yodo1MasError *)error {
[self sendEvent:REWARD_LOAD_FAILURE error:error];
}
- (void)onRewardAdOpened:(Yodo1MasRewardAd *)ad {
[self sendEvent:REWARD_OPENED_SUCCESS error:nil];
}
- (void)onRewardAdFailedToOpen:(Yodo1MasRewardAd *)ad withError:(Yodo1MasError *)error {
[self sendEvent:REWARD_LOAD_FAILURE error:error];
}
- (void)onRewardAdClosed:(Yodo1MasRewardAd *)ad {
[self sendEvent:REWARD_CLOSED error:nil];
}
- (void)onRewardAdEarned:(Yodo1MasRewardAd *)ad {
[self sendEvent:REWARD_EARNED error:nil];
}
RCT_EXPORT_MODULE(Yodo1MASAds);
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment