From 4be5993b840446b271a800b78902c741c00f79ff Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Fri, 10 Dec 2021 16:09:33 +0100 Subject: [PATCH] refactor: rewarded ads v8 --- .../RNGoogleAdsFullScreenContentDelegate.h | 6 +- .../RNGoogleAdsFullScreenContentDelegate.m | 49 +++---- .../RNGoogleAdsInterstitialModule.m | 1 + ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.h | 36 ----- ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.m | 95 ------------- ios/RNGoogleAds/RNGoogleAdsRewardedModule.m | 130 ++++++++++-------- 6 files changed, 97 insertions(+), 220 deletions(-) delete mode 100644 ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.h delete mode 100644 ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.m diff --git a/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.h b/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.h index 7ae9e185..75b50d3d 100644 --- a/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.h +++ b/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.h @@ -25,11 +25,7 @@ @interface RNGoogleAdsFullScreenContentDelegate : NSObject -+ (void)sendInterstitialEvent:(NSString *)type - requestId:(NSNumber *)requestId - adUnitId:(NSString *)adUnitId - error:(nullable NSDictionary *)error; - +@property(nonatomic, copy) NSString *sendAdEvent; @property(nonatomic, copy) NSNumber *requestId; @property(nonatomic, copy) NSString *adUnitId; diff --git a/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.m b/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.m index 963110df..08f8beaa 100644 --- a/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.m +++ b/ios/RNGoogleAds/RNGoogleAdsFullScreenContentDelegate.m @@ -20,47 +20,38 @@ @implementation RNGoogleAdsFullScreenContentDelegate #pragma mark - -#pragma mark Helper Methods +#pragma mark GADFullScreenContentDelegate Methods -+ (void)sendInterstitialEvent:(NSString *)type - requestId:(NSNumber *)requestId - adUnitId:(NSString *)adUnitId - error:(nullable NSDictionary *)error { - [RNGoogleAdsCommon sendAdEvent:GOOGLE_ADS_EVENT_INTERSTITIAL - requestId:requestId - type:type - adUnitId:adUnitId - error:error +/// Tells the delegate that the ad presented full screen content. +- (void)adDidPresentFullScreenContent:(nonnull id)ad { + [RNGoogleAdsCommon sendAdEvent:_sendAdEvent + requestId:_requestId + type:GOOGLE_ADS_EVENT_OPENED + adUnitId:_adUnitId + error:nil data:nil]; } -#pragma mark - -#pragma mark GADFullScreenContentDelegate Methods - /// Tells the delegate that the ad failed to present full screen content. - (void)ad:(nonnull id)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error { NSDictionary *codeAndMessage = [RNGoogleAdsCommon getCodeAndMessageFromAdError:error]; - [RNGoogleAdsFullScreenContentDelegate sendInterstitialEvent:GOOGLE_ADS_EVENT_ERROR - requestId:_requestId - adUnitId:_adUnitId - error:codeAndMessage]; -} - -/// Tells the delegate that the ad presented full screen content. -- (void)adDidPresentFullScreenContent:(nonnull id)ad { - [RNGoogleAdsFullScreenContentDelegate sendInterstitialEvent:GOOGLE_ADS_EVENT_OPENED - requestId:_requestId - adUnitId:_adUnitId - error:nil]; + [RNGoogleAdsCommon sendAdEvent:_sendAdEvent + requestId:_requestId + type:GOOGLE_ADS_EVENT_ERROR + adUnitId:_adUnitId + error:codeAndMessage + data:nil]; } /// Tells the delegate that the ad dismissed full screen content. - (void)adDidDismissFullScreenContent:(nonnull id)ad { - [RNGoogleAdsFullScreenContentDelegate sendInterstitialEvent:GOOGLE_ADS_EVENT_CLOSED - requestId:_requestId - adUnitId:_adUnitId - error:nil]; + [RNGoogleAdsCommon sendAdEvent:_sendAdEvent + requestId:_requestId + type:GOOGLE_ADS_EVENT_CLOSED + adUnitId:_adUnitId + error:nil + data:nil]; } @end diff --git a/ios/RNGoogleAds/RNGoogleAdsInterstitialModule.m b/ios/RNGoogleAds/RNGoogleAdsInterstitialModule.m index 327c9233..61ccb1ba 100644 --- a/ios/RNGoogleAds/RNGoogleAdsInterstitialModule.m +++ b/ios/RNGoogleAds/RNGoogleAdsInterstitialModule.m @@ -82,6 +82,7 @@ - (void)invalidate { } GADInterstitialAd *interstitial = ad; RNGoogleAdsFullScreenContentDelegate *fullScreenContentDelegate = [[RNGoogleAdsFullScreenContentDelegate alloc] init]; + fullScreenContentDelegate.sendAdEvent = GOOGLE_ADS_EVENT_INTERSTITIAL; fullScreenContentDelegate.requestId = requestId; fullScreenContentDelegate.adUnitId = ad.adUnitID; interstitial.fullScreenContentDelegate = fullScreenContentDelegate; diff --git a/ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.h b/ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.h deleted file mode 100644 index 91e114ad..00000000 --- a/ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.h +++ /dev/null @@ -1,36 +0,0 @@ -// -/** - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#import - -@import GoogleMobileAds; -#import - -#import "RNGoogleAdsCommon.h" - -@interface RNGoogleAdsRewardedDelegate : NSObject - -+ (_Nonnull instancetype)sharedInstance; - -+ (void)sendRewardedEvent:(NSString *)type - requestId:(NSNumber *)requestId - adUnitId:(NSString *)adUnitId - error:(nullable NSDictionary *)error - data:(nullable NSDictionary *)data; - -@end diff --git a/ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.m b/ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.m deleted file mode 100644 index 3c4ef45a..00000000 --- a/ios/RNGoogleAds/RNGoogleAdsRewardedDelegate.m +++ /dev/null @@ -1,95 +0,0 @@ -// -/** - * Copyright (c) 2016-present Invertase Limited & Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this library except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#import "RNGoogleAdsRewardedDelegate.h" - -@implementation RNGoogleAdsRewardedDelegate - -+ (instancetype)sharedInstance { - static dispatch_once_t once; - static RNGoogleAdsRewardedDelegate *sharedInstance; - dispatch_once(&once, ^{ - sharedInstance = [[RNGoogleAdsRewardedDelegate alloc] init]; - }); - return sharedInstance; -} - -#pragma mark - -#pragma mark Helper Methods - -+ (void)sendRewardedEvent:(NSString *)type - requestId:(NSNumber *)requestId - adUnitId:(NSString *)adUnitId - error:(nullable NSDictionary *)error - data:(nullable NSDictionary *)data { - [RNGoogleAdsCommon sendAdEvent:GOOGLE_ADS_EVENT_REWARDED - requestId:requestId - type:type - adUnitId:adUnitId - error:error - data:data]; -} - -#pragma mark - -#pragma mark GADInterstitialDelegate Methods - -/// Tells the delegate that the user earned a reward. -- (void)rewardedAd:(GADRewardedAd *)ad userDidEarnReward:(GADAdReward *)reward { - NSDictionary *data = @{ - @"type" : reward.type, - @"amount" : reward.amount, - }; - [RNGoogleAdsRewardedDelegate sendRewardedEvent:GOOGLE_ADS_EVENT_REWARDED_EARNED_REWARD - requestId:[(RNGADRewarded *)ad requestId] - adUnitId:ad.adUnitID - error:nil - data:data]; -} - -/// Tells the delegate that the rewarded ad was presented. -- (void)rewardedAdDidPresent:(GADRewardedAd *)ad { - [RNGoogleAdsRewardedDelegate sendRewardedEvent:GOOGLE_ADS_EVENT_OPENED - requestId:[(RNGADRewarded *)ad requestId] - adUnitId:ad.adUnitID - error:nil - data:nil]; -} - -/// Tells the delegate that the rewarded ad failed to present. -- (void)rewardedAd:(GADRewardedAd *)ad didFailToPresentWithError:(NSError *)error { - NSMutableDictionary *userError = [@{ - @"code" : @"unknown", - @"message" : error.localizedDescription, - } mutableCopy]; - [RNGoogleAdsRewardedDelegate sendRewardedEvent:GOOGLE_ADS_EVENT_ERROR - requestId:[(RNGADRewarded *)ad requestId] - adUnitId:ad.adUnitID - error:userError - data:nil]; -} - -/// Tells the delegate that the rewarded ad was dismissed. -- (void)rewardedAdDidDismiss:(GADRewardedAd *)ad { - [RNGoogleAdsRewardedDelegate sendRewardedEvent:GOOGLE_ADS_EVENT_CLOSED - requestId:[(RNGADRewarded *)ad requestId] - adUnitId:ad.adUnitID - error:nil - data:nil]; -} - -@end diff --git a/ios/RNGoogleAds/RNGoogleAdsRewardedModule.m b/ios/RNGoogleAds/RNGoogleAdsRewardedModule.m index 4cf19886..b8af4352 100644 --- a/ios/RNGoogleAds/RNGoogleAdsRewardedModule.m +++ b/ios/RNGoogleAds/RNGoogleAdsRewardedModule.m @@ -19,11 +19,12 @@ #import #import "RNGoogleAdsCommon.h" -#import "RNGoogleAdsRewardedDelegate.h" +#import "RNGoogleAdsFullScreenContentDelegate.h" #import "RNGoogleAdsRewardedModule.h" #import "common/RNSharedUtils.h" static __strong NSMutableDictionary *rewardedMap; +static __strong NSMutableDictionary *rewardedDelegateMap; @implementation RNGoogleAdsRewardedModule #pragma mark - @@ -40,6 +41,7 @@ - (id)init { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ rewardedMap = [[NSMutableDictionary alloc] init]; + rewardedDelegateMap = [[NSMutableDictionary alloc] init]; }); return self; } @@ -54,9 +56,8 @@ - (void)dealloc { - (void)invalidate { for (NSNumber *id in [rewardedMap allKeys]) { - RNGADRewarded *ad = rewardedMap[id]; - [ad setRequestId:@-1]; [rewardedMap removeObjectForKey:id]; + [rewardedDelegateMap removeObjectForKey:id]; } } @@ -67,54 +68,62 @@ - (void)invalidate { : (nonnull NSNumber *)requestId : (NSString *)adUnitId : (NSDictionary *)adRequestOptions) { - RNGADRewarded *rewarded = [[RNGADRewarded alloc] initWithAdUnitID:adUnitId]; - - NSDictionary *serverSideVerificationOptions = + GADRequest *request = [GADRequest request]; + [GADRewardedAd loadWithAdUnitID:adUnitId + request:request + completionHandler:^(GADRewardedAd *ad, NSError *error) { + if (error) { + NSDictionary *codeAndMessage = [RNGoogleAdsCommon getCodeAndMessageFromAdError:error]; + [RNGoogleAdsCommon sendAdEvent:GOOGLE_ADS_EVENT_REWARDED + requestId:requestId + type:GOOGLE_ADS_EVENT_ERROR + adUnitId:adUnitId + error:codeAndMessage + data:nil]; + return; + } + GADRewardedAd *rewardedAd = ad; + + NSDictionary *serverSideVerificationOptions = [adRequestOptions objectForKey:@"serverSideVerificationOptions"]; - if (serverSideVerificationOptions != nil) { - GADServerSideVerificationOptions *options = [[GADServerSideVerificationOptions alloc] init]; + if (serverSideVerificationOptions != nil) { + GADServerSideVerificationOptions *options = [[GADServerSideVerificationOptions alloc] init]; - NSString *userId = [serverSideVerificationOptions valueForKey:@"userId"]; + NSString *userId = [serverSideVerificationOptions valueForKey:@"userId"]; - if (userId != nil) { - options.userIdentifier = userId; - } + if (userId != nil) { + options.userIdentifier = userId; + } - NSString *customData = [serverSideVerificationOptions valueForKey:@"customData"]; + NSString *customData = [serverSideVerificationOptions valueForKey:@"customData"]; - if (customData != nil) { - options.customRewardString = customData; - } + if (customData != nil) { + options.customRewardString = customData; + } - [rewarded setServerSideVerificationOptions:options]; - } - - [rewarded setRequestId:requestId]; - GADRequest *request = [RNGoogleAdsCommon buildAdRequest:adRequestOptions]; - rewardedMap[requestId] = rewarded; - [rewarded loadRequest:request - completionHandler:^(GADRequestError *error) { - if (error != nil) { - NSDictionary *codeAndMessage = [RNGoogleAdsCommon getCodeAndMessageFromAdError:error]; - [RNGoogleAdsRewardedDelegate sendRewardedEvent:GOOGLE_ADS_EVENT_ERROR - requestId:requestId - adUnitId:adUnitId - error:codeAndMessage - data:nil]; - } else { - GADAdReward *reward = rewarded.reward; - NSDictionary *data = @{ - @"type" : reward.type, - @"amount" : reward.amount, - }; - [RNGoogleAdsRewardedDelegate sendRewardedEvent:GOOGLE_ADS_EVENT_REWARDED_LOADED - requestId:requestId - adUnitId:adUnitId - error:nil - data:data]; - } - }]; + [rewardedAd setServerSideVerificationOptions:options]; + } + + RNGoogleAdsFullScreenContentDelegate *fullScreenContentDelegate = [[RNGoogleAdsFullScreenContentDelegate alloc] init]; + fullScreenContentDelegate.sendAdEvent = GOOGLE_ADS_EVENT_REWARDED; + fullScreenContentDelegate.requestId = requestId; + fullScreenContentDelegate.adUnitId = ad.adUnitID; + rewardedAd.fullScreenContentDelegate = fullScreenContentDelegate; + rewardedMap[requestId] = rewardedAd; + rewardedDelegateMap[requestId] = fullScreenContentDelegate; + GADAdReward *reward = rewardedAd.adReward; + NSDictionary *data = @{ + @"type" : reward.type, + @"amount" : reward.amount, + }; + [RNGoogleAdsCommon sendAdEvent:GOOGLE_ADS_EVENT_REWARDED + requestId:requestId + type:GOOGLE_ADS_EVENT_REWARDED_LOADED + adUnitId:ad.adUnitID + error:nil + data:data]; + }]; } RCT_EXPORT_METHOD(rewardedShow @@ -123,19 +132,30 @@ - (void)invalidate { : (NSDictionary *)showOptions : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) { - GADRewardedAd *rewarded = rewardedMap[requestId]; - if (rewarded.isReady) { - [rewarded - presentFromRootViewController:RCTSharedApplication().delegate.window.rootViewController - delegate:[RNGoogleAdsRewardedDelegate sharedInstance]]; - resolve([NSNull null]); + GADRewardedAd *rewardedAd = rewardedMap[requestId]; + + if (rewardedAd) { + [rewardedAd presentFromRootViewController:RCTSharedApplication().delegate.window.rootViewController + userDidEarnRewardHandler:^ { + GADAdReward *reward = rewardedAd.adReward; + NSDictionary *data = @{ + @"type" : reward.type, + @"amount" : reward.amount, + }; + [RNGoogleAdsCommon sendAdEvent:GOOGLE_ADS_EVENT_REWARDED + requestId:requestId + type:GOOGLE_ADS_EVENT_REWARDED_EARNED_REWARD + adUnitId:rewardedAd.adUnitID + error:nil + data:data]; + }]; } else { [RNSharedUtils - rejectPromiseWithUserInfo:reject - userInfo:[@{ - @"code" : @"not-ready", - @"message" : @"Rewarded ad attempted to show but was not ready.", - } mutableCopy]]; + rejectPromiseWithUserInfo:reject + userInfo:[@{ + @"code" : @"not-ready", + @"message" : @"Rewarded ad attempted to show but was not ready.", + } mutableCopy]]; } }