#import "GADMediationCustomAdapter.h"
@interface GADMediationHandler()
@property (nonatomic, strong) GADMediationAdapterSetUpCompletionBlock setUpCompletion;
@property (nonatomic, strong) GADMediationRewardedLoadCompletionHandler loadCompletion;
@property (nonatomic, weak, nullable) id<GADMediationRewardedAdEventDelegate> rewardedAdEventDelegate;
@property (nonatomic, strong) id<GADMediationRewardedAd> rewardedAd;
@property bool initialed;
@property bool loaded;
@end
@implementation GADMediationHandler
@end
#if defined(__cplusplus)
extern "C"{
#endif
static GADCustomEventInitAdReques s_InitAdRequest;
static GADCustomEventLoadAdRequest s_LoadAdRequest;
static GADCustomEventShowAdRequest s_ShowAdRequest;
static NSMutableDictionary *s_MediationADMap = nil;
// ad system init
void GADCustomEvent_Init(GADCustomEventInitAdReques init, GADCustomEventLoadAdRequest load, GADCustomEventShowAdRequest show){
if (s_MediationADMap == nil) {
s_MediationADMap = [[NSMutableDictionary alloc] initWithCapacity:2];
}
s_InitAdRequest = init;
s_LoadAdRequest = load;
s_ShowAdRequest = show;
NSLog(@"GADMediation init.");
}
// third ad init response
void GADCustomEvent_InitAdResponse() {
for (NSString* key in s_MediationADMap) {
GADMediationHandler *handler = s_MediationADMap[key];
handler.initialed = true;
handler.setUpCompletion(nil);
}
NSLog(@"GADMediation InitAd: %lu", (unsigned long)s_MediationADMap.count);
}
// third ad response
// open=1,clcick=2,close=3,given=4,abandon=5,loaded=6
void GADCustomEvent_StateResponse(const char* name, int state, int errCode){
NSString *adName = [NSString stringWithUTF8String:name];
GADMediationHandler *handler = s_MediationADMap[adName];
if (handler != nil) {
switch (state) {
case 1:
[handler.rewardedAdEventDelegate willPresentFullScreenView];
[handler.rewardedAdEventDelegate reportImpression];
[handler.rewardedAdEventDelegate didStartVideo];
NSLog(@"GADMediation open: %@", adName);
break;
case 2:
[handler.rewardedAdEventDelegate reportClick];
NSLog(@"GADMediation click: %@", adName);
break;
case 3:
[handler.rewardedAdEventDelegate willDismissFullScreenView];
NSLog(@"GADMediation close: %@", adName);
break;
case 4:
{
[handler.rewardedAdEventDelegate didEndVideo];
GADAdReward *reward = [[GADAdReward alloc] initWithRewardType:@"reward" rewardAmount:[NSDecimalNumber decimalNumberWithString:@"1"]];
[handler.rewardedAdEventDelegate didRewardUserWithReward:reward];
NSLog(@"GADMediation given: %@", adName);
break;
}
case 6:
// success to load
if (errCode == 0) {
handler.loaded = true;
handler.rewardedAdEventDelegate = handler.loadCompletion(handler.rewardedAd, nil);
NSLog(@"GADMediation load success: %@", adName);
} else {
// failed
handler.loaded = false;
handler.loadCompletion(nil, [[NSError alloc] initWithDomain:NSMachErrorDomain code:1 userInfo:nil]);
[s_MediationADMap removeObjectForKey:adName];
NSLog(@"GADMediation load failed: %@", adName);
}
break;
}
}
}
#if defined(__cplusplus)
}
#endif
@interface GADMediationCustomAdapter() <GADMediationRewardedAd>
@end
@implementation GADMediationCustomAdapter
+ (GADVersionNumber)adSDKVersion {
NSString *versionString = @"1.0.0";
NSArray *versionComponents = [versionString componentsSeparatedByString:@"."];
GADVersionNumber version = {0};
if (versionComponents.count == 3) {
version.majorVersion = [versionComponents[0] integerValue];
version.minorVersion = [versionComponents[1] integerValue];
version.patchVersion = [versionComponents[2] integerValue];
}
return version;
}
+ (GADVersionNumber)version {
NSString *versionString = @"1.0.0.0";
NSArray *versionComponents = [versionString componentsSeparatedByString:@"."];
GADVersionNumber version = {0};
if (versionComponents.count == 4) {
version.majorVersion = [versionComponents[0] integerValue];
version.minorVersion = [versionComponents[1] integerValue];
// Adapter versions have 2 patch versions. Multiply the first patch by 100.
version.patchVersion = [versionComponents[2] integerValue] * 100
+ [versionComponents[3] integerValue];
}
return version;
}
+ (nullable Class<GADAdNetworkExtras>)networkExtrasClass {
return Nil;
}
// init third ad
+ (void)setUpWithConfiguration:(nonnull GADMediationServerConfiguration *)configuration
completionHandler:(nonnull GADMediationAdapterSetUpCompletionBlock)completionHandler{
for (GADMediationCredentials *credential in configuration.credentials){
if (credential.format == GADAdFormatRewarded){
NSString *adName = credential.settings[@"parameter"];
GADMediationHandler *handler = [[GADMediationHandler alloc] init];
handler.setUpCompletion = completionHandler;
handler.initialed = false;
handler.loaded = false;
[s_MediationADMap setObject:handler forKey:adName];
NSLog(@"GADMediation setup: %@", adName);
}
}
s_InitAdRequest();
NSLog(@"GADMediation initAdRequest");
}
// load third ad
- (void)loadRewardedAdForAdConfiguration:(nonnull GADMediationRewardedAdConfiguration *)adConfiguration completionHandler:(nonnull GADMediationRewardedLoadCompletionHandler) completionHandler{
NSString *adName = adConfiguration.credentials.settings[@"parameter"];
GADMediationHandler *handler = s_MediationADMap[adName];
if (handler != nil) {
handler.loadCompletion = completionHandler;
handler.rewardedAd = self;
s_LoadAdRequest([adName UTF8String]);
NSLog(@"GADMediation load: %@", adName);
} else {
completionHandler(nil, [[NSError alloc] initWithDomain:NSArgumentDomain code:1 userInfo:nil]);
NSLog(@"GADMediation load err: NoExistName, name: %@", adName);
}
}
// show third ad
- (void)presentFromViewController:(nonnull UIViewController *)viewController {
// found frist available ad
for (NSString *key in s_MediationADMap) {
GADMediationHandler *handler = s_MediationADMap[key];
if (handler.loaded) {
s_ShowAdRequest([key UTF8String]);
NSLog(@"GADMediation show: %@", key);
return;
}
}
NSLog(@"GADMediation show err: AllNotReady");
}
@end