2017-05-27 14:24:51 +00:00
|
|
|
|
#import "RNFirebaseAdMob.h"
|
2017-06-20 12:22:09 +00:00
|
|
|
|
|
|
|
|
|
#if __has_include(<GoogleMobileAds/GADMobileAds.h>)
|
|
|
|
|
|
2017-06-05 16:00:59 +00:00
|
|
|
|
#import "GoogleMobileAds/GADMobileAds.h"
|
|
|
|
|
|
|
|
|
|
#import "RNFirebaseAdMobInterstitial.h"
|
2017-06-07 13:19:06 +00:00
|
|
|
|
#import "RNFirebaseAdMobRewardedVideo.h"
|
2017-05-27 14:24:51 +00:00
|
|
|
|
|
2017-06-07 11:37:32 +00:00
|
|
|
|
NSString *MALE = @"male";
|
|
|
|
|
NSString *FEMALE = @"female";
|
|
|
|
|
NSString *UNKNOWN = @"unknown";
|
|
|
|
|
|
2017-05-27 14:24:51 +00:00
|
|
|
|
@implementation RNFirebaseAdMob
|
|
|
|
|
RCT_EXPORT_MODULE();
|
2017-05-27 19:46:17 +00:00
|
|
|
|
|
2017-06-05 16:00:59 +00:00
|
|
|
|
- (id)init {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self != nil) {
|
|
|
|
|
_interstitials = [[NSMutableDictionary alloc] init];
|
2017-06-07 13:19:06 +00:00
|
|
|
|
_rewardedVideos = [[NSMutableDictionary alloc] init];
|
2017-06-05 16:00:59 +00:00
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (dispatch_queue_t)methodQueue {
|
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 11:37:32 +00:00
|
|
|
|
RCT_EXPORT_METHOD(initialize:
|
|
|
|
|
(NSString *) appId) {
|
2017-06-05 16:00:59 +00:00
|
|
|
|
[GADMobileAds configureWithApplicationID:appId];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 14:14:02 +00:00
|
|
|
|
RCT_EXPORT_METHOD(openDebugMenu:
|
|
|
|
|
(NSString *) appId) {
|
|
|
|
|
GADDebugOptionsViewController *debugOptionsViewController = [GADDebugOptionsViewController debugOptionsViewControllerWithAdUnitID:appId];
|
2017-07-04 14:39:56 +00:00
|
|
|
|
UIWindow *window = [UIApplication sharedApplication].keyWindow;
|
2017-06-27 14:14:02 +00:00
|
|
|
|
|
|
|
|
|
[[window rootViewController] presentViewController:debugOptionsViewController animated:YES completion:nil];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 13:19:06 +00:00
|
|
|
|
RCT_EXPORT_METHOD(interstitialLoadAd:
|
|
|
|
|
(NSString *) adUnit
|
|
|
|
|
request:
|
|
|
|
|
(NSDictionary *) request) {
|
|
|
|
|
RNFirebaseAdMobInterstitial *interstitial = [self getOrCreateInterstitial:adUnit];
|
|
|
|
|
[interstitial loadAd:request];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 11:37:32 +00:00
|
|
|
|
RCT_EXPORT_METHOD(interstitialShowAd:
|
|
|
|
|
(NSString *) adUnit) {
|
|
|
|
|
RNFirebaseAdMobInterstitial *interstitial = [self getOrCreateInterstitial:adUnit];
|
2017-06-07 13:19:06 +00:00
|
|
|
|
[interstitial show];
|
2017-06-05 16:00:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 13:19:06 +00:00
|
|
|
|
RCT_EXPORT_METHOD(rewardedVideoLoadAd:
|
2017-06-07 11:37:32 +00:00
|
|
|
|
(NSString *) adUnit
|
|
|
|
|
request:
|
|
|
|
|
(NSDictionary *) request) {
|
2017-06-07 13:19:06 +00:00
|
|
|
|
RNFirebaseAdMobRewardedVideo *rewardedVideo = [self getOrCreateRewardedVideo:adUnit];
|
|
|
|
|
[rewardedVideo loadAd:request];
|
2017-06-05 16:00:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 13:19:06 +00:00
|
|
|
|
RCT_EXPORT_METHOD(rewardedVideoShowAd:
|
|
|
|
|
(NSString *) adUnit) {
|
|
|
|
|
RNFirebaseAdMobRewardedVideo *rewardedVideo = [self getOrCreateRewardedVideo:adUnit];
|
|
|
|
|
[rewardedVideo show];
|
|
|
|
|
}
|
2017-06-05 16:00:59 +00:00
|
|
|
|
|
2017-07-04 14:39:56 +00:00
|
|
|
|
RCT_EXPORT_METHOD(clearInterstitial:
|
|
|
|
|
(NSString *) adUnit) {
|
|
|
|
|
if (_interstitials[adUnit]) [_interstitials removeObjectForKey:adUnit];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 11:37:32 +00:00
|
|
|
|
- (RNFirebaseAdMobInterstitial *)getOrCreateInterstitial:(NSString *)adUnit {
|
2017-06-05 16:00:59 +00:00
|
|
|
|
if (_interstitials[adUnit]) {
|
|
|
|
|
return _interstitials[adUnit];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 11:37:32 +00:00
|
|
|
|
_interstitials[adUnit] = [[RNFirebaseAdMobInterstitial alloc] initWithProps:adUnit delegate:self];
|
2017-06-05 16:00:59 +00:00
|
|
|
|
return _interstitials[adUnit];
|
|
|
|
|
}
|
2017-05-31 14:22:15 +00:00
|
|
|
|
|
2017-06-07 13:19:06 +00:00
|
|
|
|
- (RNFirebaseAdMobRewardedVideo *)getOrCreateRewardedVideo:(NSString *)adUnit {
|
|
|
|
|
if (_rewardedVideos[adUnit]) {
|
|
|
|
|
return _rewardedVideos[adUnit];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rewardedVideos[adUnit] = [[RNFirebaseAdMobRewardedVideo alloc] initWithProps:adUnit delegate:self];
|
|
|
|
|
return _rewardedVideos[adUnit];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-05 16:00:59 +00:00
|
|
|
|
- (NSArray<NSString *> *)supportedEvents {
|
|
|
|
|
return @[ADMOB_INTERSTITIAL_EVENT, ADMOB_REWARDED_VIDEO_EVENT];
|
2017-05-27 19:46:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-16 14:21:49 +00:00
|
|
|
|
+ (GADRequest *)buildRequest:(NSDictionary *)request {
|
2017-06-07 11:37:32 +00:00
|
|
|
|
GADRequest *builder = [GADRequest request];
|
|
|
|
|
|
|
|
|
|
if (request[@"tagForChildDirectedTreatment"]) [builder tagForChildDirectedTreatment:(BOOL) request[@"tagForChildDirectedTreatment"]];
|
|
|
|
|
if (request[@"contentUrl"]) builder.contentURL = request[@"contentUrl"];
|
|
|
|
|
if (request[@"requestAgent"]) builder.requestAgent = request[@"requestAgent"];
|
|
|
|
|
if (request[@"keywords"]) builder.keywords = request[@"keywords"];
|
2017-06-16 14:21:49 +00:00
|
|
|
|
|
|
|
|
|
if (request[@"testDevices"]) {
|
2017-06-16 16:40:05 +00:00
|
|
|
|
NSArray *devices = request[@"testDevices"];
|
|
|
|
|
NSMutableArray *testDevices = [[NSMutableArray alloc] init];
|
2017-06-16 14:21:49 +00:00
|
|
|
|
|
|
|
|
|
for (id device in devices) {
|
|
|
|
|
if ([device isEqual:@"DEVICE_ID_EMULATOR"]) {
|
|
|
|
|
[testDevices addObject:kGADSimulatorID];
|
|
|
|
|
} else {
|
|
|
|
|
[testDevices addObject:device];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.testDevices = testDevices;
|
|
|
|
|
}
|
2017-06-07 11:37:32 +00:00
|
|
|
|
|
|
|
|
|
if (request[@"gender"]) {
|
|
|
|
|
NSString *gender = [request[@"gender"] stringValue];
|
|
|
|
|
|
|
|
|
|
if (gender == MALE) {
|
|
|
|
|
builder.gender = kGADGenderMale;
|
|
|
|
|
} else if (gender == FEMALE) {
|
|
|
|
|
builder.gender = kGADGenderFemale;
|
|
|
|
|
} else if (gender == UNKNOWN) {
|
|
|
|
|
builder.gender = kGADGenderUnknown;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-16 16:40:05 +00:00
|
|
|
|
+ (GADVideoOptions *)buildVideoOptions:(NSDictionary *)options {
|
|
|
|
|
GADVideoOptions *builder = [[GADVideoOptions alloc] init];
|
|
|
|
|
builder.startMuted = (BOOL) options[@"startMuted"];
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-16 14:21:49 +00:00
|
|
|
|
+ (NSDictionary *)errorCodeToDictionary:(NSError *)error {
|
2017-06-07 11:37:32 +00:00
|
|
|
|
NSString *code;
|
|
|
|
|
NSString *message;
|
|
|
|
|
|
|
|
|
|
switch (error.code) {
|
|
|
|
|
default:
|
|
|
|
|
case kGADErrorServerError:
|
|
|
|
|
case kGADErrorInternalError:
|
|
|
|
|
case kGADErrorInvalidArgument:
|
|
|
|
|
case kGADErrorMediationDataError:
|
|
|
|
|
case kGADErrorMediationAdapterError:
|
|
|
|
|
code = @"admob/error-code-internal-error ";
|
|
|
|
|
message = @"Something happened internally; for instance, an invalid response was received from the ad server.";
|
|
|
|
|
break;
|
|
|
|
|
case kGADErrorMediationInvalidAdSize:
|
|
|
|
|
code = @"admob/error-code-invalid-request";
|
|
|
|
|
message = @"The ad requested has an invalid size.";
|
|
|
|
|
break;
|
|
|
|
|
case kGADErrorInvalidRequest:
|
|
|
|
|
case kGADErrorReceivedInvalidResponse:
|
|
|
|
|
code = @"admob/error-code-invalid-request";
|
|
|
|
|
message = @"The ad request was invalid; for instance, the ad unit ID was incorrect.";
|
|
|
|
|
break;
|
|
|
|
|
case kGADErrorNoFill:
|
|
|
|
|
case kGADErrorMediationNoFill:
|
|
|
|
|
code = @"admob/error-code-no-fill";
|
|
|
|
|
message = @"The ad request was successful, but no ad was returned due to lack of ad inventory.";
|
|
|
|
|
break;
|
|
|
|
|
case kGADErrorNetworkError:
|
|
|
|
|
code = @"admob/error-code-network-error";
|
|
|
|
|
message = @"The ad request was unsuccessful due to network connectivity.";
|
|
|
|
|
break;
|
|
|
|
|
case kGADErrorOSVersionTooLow:
|
|
|
|
|
code = @"admob/os-version-too-low";
|
|
|
|
|
message = @"The current device’s OS is below the minimum required version.";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return @{
|
|
|
|
|
@"code": code,
|
|
|
|
|
@"message": message,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-16 14:21:49 +00:00
|
|
|
|
+ (GADAdSize)stringToAdSize:(NSString *)value {
|
2017-06-16 16:40:05 +00:00
|
|
|
|
NSError *error = nil;
|
|
|
|
|
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([0-9]+)x([0-9]+)" options:0 error:&error];
|
|
|
|
|
NSArray *matches = [regex matchesInString:value options:0 range:NSMakeRange(0, [value length])];
|
|
|
|
|
|
|
|
|
|
for (NSTextCheckingResult *match in matches) {
|
|
|
|
|
NSString *matchText = [value substringWithRange:[match range]];
|
|
|
|
|
if (matchText) {
|
|
|
|
|
NSArray *values = [matchText componentsSeparatedByString:@"x"];
|
2017-07-04 14:39:56 +00:00
|
|
|
|
CGFloat width = (CGFloat) [values[0] intValue];
|
|
|
|
|
CGFloat height = (CGFloat) [values[1] intValue];
|
2017-06-16 16:40:05 +00:00
|
|
|
|
return GADAdSizeFromCGSize(CGSizeMake(width, height));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value = [value uppercaseString];
|
|
|
|
|
|
2017-06-16 14:21:49 +00:00
|
|
|
|
if ([value isEqualToString:@"BANNER"]) {
|
|
|
|
|
return kGADAdSizeBanner;
|
|
|
|
|
} else if ([value isEqualToString:@"LARGE_BANNER"]) {
|
|
|
|
|
return kGADAdSizeLargeBanner;
|
|
|
|
|
} else if ([value isEqualToString:@"MEDIUM_RECTANGLE"]) {
|
|
|
|
|
return kGADAdSizeMediumRectangle;
|
|
|
|
|
} else if ([value isEqualToString:@"FULL_BANNER"]) {
|
|
|
|
|
return kGADAdSizeFullBanner;
|
|
|
|
|
} else if ([value isEqualToString:@"LEADERBOARD"]) {
|
|
|
|
|
return kGADAdSizeLeaderboard;
|
|
|
|
|
} else if ([value isEqualToString:@"SMART_BANNER"]) {
|
|
|
|
|
return kGADAdSizeSmartBannerPortrait;
|
|
|
|
|
} else {
|
|
|
|
|
return kGADAdSizeBanner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 14:24:51 +00:00
|
|
|
|
@end
|
2017-06-20 12:22:09 +00:00
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
@interface RNFirebaseAdMobRewardedVideo : NSObject <RCTBridgeModule> {
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#endif
|