react-native-google-mobile-ads 6.2.4 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/RNGoogleMobileAds.podspec +1 -1
  2. package/android/build.gradle +23 -3
  3. package/android/src/main/AndroidManifest.xml +6 -0
  4. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsAdHelper.kt +106 -0
  5. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsAppOpenModule.kt +74 -0
  6. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsCommon.java +5 -0
  7. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsFullScreenAdModule.kt +245 -0
  8. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsInterstitialModule.kt +74 -0
  9. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsModule.java +2 -1
  10. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedInterstitialModule.kt +74 -0
  11. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedModule.kt +74 -0
  12. package/docs/displaying-ads.mdx +2 -2
  13. package/docs/european-user-consent.mdx +3 -2
  14. package/docs/index.mdx +5 -1
  15. package/docs/migrating-to-v6.mdx +2 -2
  16. package/ios/RNGoogleMobileAds/RNGoogleMobileAds-Bridging-Header.h +5 -0
  17. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.m +13 -68
  18. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.swift +82 -0
  19. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsCommon.m +4 -0
  20. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenAd.swift +208 -0
  21. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenContentDelegate.swift +72 -0
  22. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsInterstitialModule.m +13 -107
  23. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsInterstitialModule.swift +119 -0
  24. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsModule.m +2 -1
  25. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedInterstitialModule.m +13 -144
  26. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedInterstitialModule.swift +81 -0
  27. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedModule.m +13 -143
  28. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedModule.swift +81 -0
  29. package/ios_config.sh +10 -0
  30. package/lib/commonjs/ads/MobileAd.js +1 -1
  31. package/lib/commonjs/ads/MobileAd.js.map +1 -1
  32. package/lib/commonjs/validateAdRequestOptions.js +8 -0
  33. package/lib/commonjs/validateAdRequestOptions.js.map +1 -1
  34. package/lib/commonjs/version.js +1 -1
  35. package/lib/commonjs/version.js.map +1 -1
  36. package/lib/module/ads/MobileAd.js +1 -1
  37. package/lib/module/ads/MobileAd.js.map +1 -1
  38. package/lib/module/validateAdRequestOptions.js +8 -0
  39. package/lib/module/validateAdRequestOptions.js.map +1 -1
  40. package/lib/module/version.js +1 -1
  41. package/lib/module/version.js.map +1 -1
  42. package/lib/typescript/index.d.ts +1 -1
  43. package/lib/typescript/types/RequestOptions.d.ts +6 -0
  44. package/lib/typescript/version.d.ts +1 -1
  45. package/package.json +4 -4
  46. package/src/ads/MobileAd.ts +2 -1
  47. package/src/types/RequestOptions.ts +7 -0
  48. package/src/validateAdRequestOptions.ts +7 -0
  49. package/src/version.ts +1 -1
  50. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsAppOpenModule.java +0 -159
  51. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsInterstitialModule.java +0 -178
  52. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedInterstitialModule.java +0 -199
  53. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedModule.java +0 -176
  54. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.h +0 -29
  55. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenContentDelegate.h +0 -33
  56. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenContentDelegate.m +0 -72
  57. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsInterstitialModule.h +0 -25
  58. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedInterstitialModule.h +0 -25
  59. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedModule.h +0 -25
@@ -0,0 +1,74 @@
1
+ package io.invertase.googlemobileads
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import android.app.Activity
21
+ import com.facebook.react.bridge.Promise
22
+ import com.facebook.react.bridge.ReactApplicationContext
23
+ import com.facebook.react.bridge.ReactMethod
24
+ import com.facebook.react.bridge.ReadableMap
25
+ import com.google.android.gms.ads.AdLoadCallback
26
+ import com.google.android.gms.ads.LoadAdError
27
+ import com.google.android.gms.ads.admanager.AdManagerAdRequest
28
+ import com.google.android.gms.ads.rewarded.RewardedAd
29
+ import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
30
+
31
+ class ReactNativeGoogleMobileAdsRewardedModule(reactContext: ReactApplicationContext?) :
32
+ ReactNativeGoogleMobileAdsFullScreenAdModule<RewardedAd>(reactContext, NAME) {
33
+
34
+ override fun getAdEventName(): String {
35
+ return ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_REWARDED
36
+ }
37
+
38
+ @ReactMethod
39
+ fun rewardedLoad(requestId: Int, adUnitId: String, adRequestOptions: ReadableMap) {
40
+ load(requestId, adUnitId, adRequestOptions)
41
+ }
42
+
43
+ @ReactMethod
44
+ fun rewardedShow(
45
+ requestId: Int, adUnitId: String, showOptions: ReadableMap, promise: Promise
46
+ ) {
47
+ show(requestId, adUnitId, showOptions, promise)
48
+ }
49
+
50
+ override fun loadAd(
51
+ activity: Activity,
52
+ adUnitId: String,
53
+ adRequest: AdManagerAdRequest,
54
+ adLoadCallback: AdLoadCallback<RewardedAd>
55
+ ) {
56
+ RewardedAd.load(
57
+ activity,
58
+ adUnitId,
59
+ adRequest,
60
+ object :
61
+ RewardedAdLoadCallback() {
62
+ override fun onAdLoaded(ad: RewardedAd) {
63
+ adLoadCallback.onAdLoaded(ad)
64
+ }
65
+ override fun onAdFailedToLoad(error: LoadAdError) {
66
+ adLoadCallback.onAdFailedToLoad(error)
67
+ }
68
+ })
69
+ }
70
+
71
+ companion object {
72
+ const val NAME = "RNGoogleMobileAdsRewardedModule"
73
+ }
74
+ }
@@ -107,7 +107,7 @@ function App() {
107
107
  const [loaded, setLoaded] = useState(false);
108
108
 
109
109
  useEffect(() => {
110
- const unsubscribe = interstitial.addAdEventListener(AdEventType.LOADED => {
110
+ const unsubscribe = interstitial.addAdEventListener(AdEventType.LOADED, () => {
111
111
  setLoaded(true);
112
112
  });
113
113
 
@@ -396,7 +396,7 @@ If you request an Advert as in the example above, AdMob will call your server wi
396
396
 
397
397
  You still need to verify these incoming requests yourself to ensure they are genuine. To learn more about callback parameters and verifying, see the [AdMob SDK Server Side Verification(SSV) documentation](https://developers.google.com/admob/android/rewarded-video-ssv).
398
398
 
399
- ## Banner Ads
399
+ ## Banner Ads (component)
400
400
 
401
401
  Banner ads are partial adverts which can be integrated within your existing application. Unlike Interstitial and Rewarded Ads,
402
402
  a Banner only takes up a limited area of the application and displays an advert within the area. This allows you to integrate
@@ -93,7 +93,7 @@ The method returns an `AdsConsentInfo` interface, which provides information abo
93
93
  - `OBTAINED`: User consent already obtained.
94
94
  - **isConsentFormAvailable**: A boolean value. If `true` a consent form is available.
95
95
 
96
- **Note:** The return status of this call is the status of *form presentation and response collection*, **not
96
+ **Note:** The return status of this call is the status of _form presentation and response collection_, **not
97
97
  the the actual user consent**. It simply indicates if you now have a consent response to decode.
98
98
  (_i.e._ if user consent is **required**, the form has been presented, and user has
99
99
  **denied** the consent, the status returned by this method will be `OBTAINED`,
@@ -197,4 +197,5 @@ UMP SDK for [Android](https://developers.google.com/admob/ump/android/quick-star
197
197
  [iOS](https://developers.google.com/admob/ump/ios/quick-start).
198
198
 
199
199
  <!-- links -->
200
- [Inspecting consent choices]: #inspecting-consent-choices
200
+
201
+ [inspecting consent choices]: #inspecting-consent-choices
package/docs/index.mdx CHANGED
@@ -7,6 +7,10 @@ yarn add react-native-google-mobile-ads
7
7
 
8
8
  > On Android, before releasing your app, you must select _Yes, my app contains ads_ in the Google Play Store, Policy, App content, Manage under Ads.
9
9
 
10
+ ## Optionally configure iOS static frameworks
11
+
12
+ On iOS if you need to use static frameworks (that is, `use_frameworks!` in your `Podfile`) you must add the variable `$RNGoogleMobileAdsAsStaticFramework = true` to the targets in your `Podfile`. You may need this if you use this module in combination with react-native-firebase v15 and higher since it requires `use_frameworks!`.
13
+
10
14
  ## What does it do
11
15
 
12
16
  The AdMob module allows you to display adverts to your users. All adverts are served over the Google AdMob network, meaning
@@ -39,7 +43,7 @@ Before you are able to display ads to your users, you must have a [Google AdMob
39
43
  "Apps" menu item, create or choose an existing Android/iOS app. Each app platform exposes a unique account ID which needs to
40
44
  be added to the project.
41
45
 
42
- > Attempting to build your app without a valid App ID may cause a crash during build.
46
+ > Attempting to build your app without a valid App ID in `app.json` will cause the app to crash on start or fail to build.
43
47
 
44
48
  Under the "App settings" menu item, you can find the "App ID":
45
49
 
@@ -32,7 +32,7 @@ With each call to `addAdEventListener`, you add a listener for one specific even
32
32
  - console.log('Ad failed to load with error: ', error);
33
33
  - }
34
34
  -}
35
- +rewardedAd.addAdEventListener(RewardedAdEventType.Loaded, () => {
35
+ +rewardedAd.addAdEventListener(RewardedAdEventType.LOADED, () => {
36
36
  + console.log('Ad has loaded');
37
37
  +});
38
38
  +rewardedAd.addAdEventListener(RewardedAdEventType.EARNED_REWARD, (reward) => {
@@ -46,7 +46,7 @@ With each call to `addAdEventListener`, you add a listener for one specific even
46
46
  To unsubscribe from an event, call the function returned from `addAdEventListener`.
47
47
 
48
48
  ```js
49
- const unsubscribe = interstitialAd.addAdEventListener(AdEventType.Loaded, () => {
49
+ const unsubscribe = interstitialAd.addAdEventListener(AdEventType.LOADED, () => {
50
50
  console.log('Ad has loaded');
51
51
  });
52
52
 
@@ -0,0 +1,5 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTEventEmitter.h>
3
+
4
+ #import "RNGoogleMobileAdsCommon.h"
5
+ #import "RNSharedUtils.h"
@@ -15,17 +15,9 @@
15
15
  *
16
16
  */
17
17
 
18
- #import <React/RCTUtils.h>
18
+ #import <React/RCTBridgeModule.h>
19
19
 
20
- #import "RNGoogleMobileAdsAppOpenModule.h"
21
- #import "RNGoogleMobileAdsCommon.h"
22
- #import "common/RNSharedUtils.h"
23
-
24
- @implementation RNGoogleMobileAdsAppOpenModule
25
- #pragma mark -
26
- #pragma mark Module Setup
27
-
28
- RCT_EXPORT_MODULE();
20
+ @interface RCT_EXTERN_MODULE (RNGoogleMobileAdsAppOpenModule, NSObject)
29
21
 
30
22
  - (dispatch_queue_t)methodQueue {
31
23
  return dispatch_get_main_queue();
@@ -35,63 +27,16 @@ RCT_EXPORT_MODULE();
35
27
  return YES;
36
28
  }
37
29
 
38
- // #pragma mark -
39
- // #pragma mark Google Mobile Ads Methods
40
-
41
- RCT_EXPORT_METHOD(appOpenLoad
42
- : (nonnull NSNumber *)requestId
43
- : (NSString *)adUnitId
44
- : (NSDictionary *)adRequestOptions) {
45
- self.appOpenAd = nil;
46
- [GADAppOpenAd loadWithAdUnitID:adUnitId
47
- request:[RNGoogleMobileAdsCommon buildAdRequest:adRequestOptions]
48
- orientation:UIInterfaceOrientationPortrait
49
- completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
50
- if (error) {
51
- NSDictionary *codeAndMessage =
52
- [RNGoogleMobileAdsCommon getCodeAndMessageFromAdError:error];
53
- [RNGoogleMobileAdsCommon sendAdEvent:GOOGLE_MOBILE_ADS_EVENT_APP_OPEN
54
- requestId:requestId
55
- type:GOOGLE_MOBILE_ADS_EVENT_ERROR
56
- adUnitId:adUnitId
57
- error:codeAndMessage
58
- data:nil];
59
- return;
60
- }
61
- self.appOpenAd = appOpenAd;
62
- RNGoogleMobileAdsFullScreenContentDelegate *fullScreenContentDelegate =
63
- [[RNGoogleMobileAdsFullScreenContentDelegate alloc] init];
64
- fullScreenContentDelegate.sendAdEvent = GOOGLE_MOBILE_ADS_EVENT_APP_OPEN;
65
- fullScreenContentDelegate.requestId = requestId;
66
- fullScreenContentDelegate.adUnitId = adUnitId;
67
- self.appOpenAd.fullScreenContentDelegate = fullScreenContentDelegate;
68
- self.appOpenDelegate = fullScreenContentDelegate;
69
- [RNGoogleMobileAdsCommon sendAdEvent:GOOGLE_MOBILE_ADS_EVENT_APP_OPEN
70
- requestId:requestId
71
- type:GOOGLE_MOBILE_ADS_EVENT_LOADED
72
- adUnitId:adUnitId
73
- error:nil
74
- data:nil];
75
- }];
76
- }
77
-
78
- RCT_EXPORT_METHOD(appOpenShow
79
- : (nonnull NSNumber *)requestId
80
- : (NSString *)adUnitId
81
- : (NSDictionary *)showOptions
82
- : (RCTPromiseResolveBlock)resolve
83
- : (RCTPromiseRejectBlock)reject) {
84
- if (self.appOpenAd) {
85
- [self.appOpenAd presentFromRootViewController:RNGoogleMobileAdsCommon.currentViewController];
86
- resolve([NSNull null]);
87
- } else {
88
- [RNSharedUtils
89
- rejectPromiseWithUserInfo:reject
90
- userInfo:[@{
91
- @"code" : @"not-ready",
92
- @"message" : @"App Open ad attempted to show but was not ready.",
93
- } mutableCopy]];
94
- }
95
- }
30
+ RCT_EXTERN_METHOD(appOpenLoad
31
+ : (nonnull NSNumber *)requestId forAdUnitId
32
+ : (nonnull NSString *)adUnitId withAdRequestOptions
33
+ : (nonnull NSDictionary *)adRequestOptions)
34
+
35
+ RCT_EXTERN_METHOD(appOpenShow
36
+ : (nonnull NSNumber *)requestId forAdUnitId
37
+ : (nonnull NSString *)adUnitId withShowOptions
38
+ : (NSDictionary *)showOptions withResolve
39
+ : (RCTPromiseResolveBlock)resolve withReject
40
+ : (RCTPromiseRejectBlock)reject)
96
41
 
97
42
  @end
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import Foundation
19
+ import GoogleMobileAds
20
+
21
+ @objc(RNGoogleMobileAdsAppOpenModule)
22
+ class RNGoogleMobileAdsAppOpenModule: NSObject {
23
+ let ad = RNGoogleMobileAdsAppOpenAd()
24
+
25
+ deinit {
26
+ invalidate()
27
+ }
28
+
29
+ @objc
30
+ func invalidate() {
31
+ ad.invalidate()
32
+ }
33
+
34
+ @objc(appOpenLoad:forAdUnitId:withAdRequestOptions:)
35
+ func appOpenLoad(
36
+ requestId: NSNumber,
37
+ adUnitId: String,
38
+ adRequestOptions: Dictionary<String, Any>
39
+ ) {
40
+ ad.load(
41
+ requestId: requestId.intValue,
42
+ adUnitId: adUnitId,
43
+ adRequestOptions: adRequestOptions
44
+ )
45
+ }
46
+
47
+ @objc(appOpenShow:forAdUnitId:withShowOptions:withResolve:withReject:)
48
+ func appOpenShow(
49
+ requestId: NSNumber,
50
+ adUnitId: String,
51
+ showOptions: Dictionary<String, Any>,
52
+ resolve: RCTPromiseResolveBlock?,
53
+ reject: RCTPromiseRejectBlock?
54
+ ) {
55
+ ad.show(
56
+ requestId: requestId.intValue,
57
+ adUnitId: adUnitId,
58
+ showOptions: showOptions,
59
+ resolve: resolve,
60
+ reject: reject
61
+ )
62
+ }
63
+
64
+ class RNGoogleMobileAdsAppOpenAd: RNGoogleMobileAdsFullScreenAd<GADAppOpenAd> {
65
+ override func getAdEventName() -> String {
66
+ return GOOGLE_MOBILE_ADS_EVENT_APP_OPEN
67
+ }
68
+
69
+ override func loadAd(
70
+ adUnitId: String,
71
+ adRequest: GAMRequest,
72
+ completionHandler: @escaping (GADAppOpenAd?, Error?) -> ()
73
+ ) {
74
+ GADAppOpenAd.load(
75
+ withAdUnitID: adUnitId,
76
+ request: adRequest,
77
+ orientation: UIInterfaceOrientation.portrait,
78
+ completionHandler: completionHandler
79
+ )
80
+ }
81
+ }
82
+ }
@@ -83,6 +83,10 @@ NSString *const GOOGLE_MOBILE_ADS_EVENT_REWARDED_EARNED_REWARD = @"rewarded_earn
83
83
  request.customTargeting = adRequestOptions[@"customTargeting"];
84
84
  }
85
85
 
86
+ if (adRequestOptions[@"publisherProvidedId"]) {
87
+ request.publisherProvidedID = adRequestOptions[@"publisherProvidedId"];
88
+ }
89
+
86
90
  return request;
87
91
  }
88
92
 
@@ -0,0 +1,208 @@
1
+ /**
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import Foundation
19
+ import GoogleMobileAds
20
+
21
+ class RNGoogleMobileAdsFullScreenAd<T>: NSObject where T : GADFullScreenPresentingAd {
22
+ var adMap = Dictionary<Int, T>()
23
+ var delegateMap = Dictionary<Int, RNGoogleMobileAdsFullScreenContentDelegate>()
24
+
25
+ deinit {
26
+ invalidate()
27
+ }
28
+
29
+ func invalidate() {
30
+ adMap.removeAll()
31
+ delegateMap.removeAll()
32
+ }
33
+
34
+ func getAdEventName() -> String {
35
+ fatalError("Method `getAdEventName` must be overriden")
36
+ }
37
+
38
+ func loadAd(
39
+ adUnitId: String,
40
+ adRequest: GAMRequest,
41
+ completionHandler: @escaping (_ ad: T?, _ error: Error?) -> ()
42
+ ) {
43
+ fatalError("Method `loadAd` must be overriden")
44
+ }
45
+
46
+ func sendAdEvent(
47
+ _ type: String,
48
+ requestId: Int,
49
+ adUnitId: String,
50
+ error: Dictionary<String, Any>?,
51
+ data: Dictionary<String, Any>?
52
+ ) {
53
+ RNGoogleMobileAdsCommon.sendAdEvent(
54
+ getAdEventName(),
55
+ requestId: requestId as NSNumber,
56
+ type: type,
57
+ adUnitId: adUnitId,
58
+ error: error,
59
+ data: data
60
+ )
61
+ }
62
+
63
+ //MARK: - Load function
64
+
65
+ func load(
66
+ requestId: Int,
67
+ adUnitId: String,
68
+ adRequestOptions: Dictionary<String, Any>
69
+ ) {
70
+ let adRequest = RNGoogleMobileAdsCommon.buildAdRequest(adRequestOptions)!
71
+
72
+ let delegate = RNGoogleMobileAdsFullScreenContentDelegate(
73
+ adEventName: getAdEventName(),
74
+ requestId: requestId,
75
+ adUnitId: adUnitId
76
+ )
77
+
78
+ let completionHandler = { (ad: T?, error: Error?) -> () in
79
+ var eventType = GOOGLE_MOBILE_ADS_EVENT_LOADED
80
+ var data: Dictionary<String, Any>? = nil
81
+ if let error = error {
82
+ let codeAndMessage = RNGoogleMobileAdsCommon.getCodeAndMessage(fromAdError: error)
83
+ self.sendAdEvent(
84
+ GOOGLE_MOBILE_ADS_EVENT_ERROR,
85
+ requestId: requestId,
86
+ adUnitId: adUnitId,
87
+ error: codeAndMessage as? Dictionary<String, Any>,
88
+ data: nil
89
+ )
90
+ return
91
+ }
92
+
93
+ if (ad is GADRewardedAd || ad is GADRewardedInterstitialAd) {
94
+ if let serverSideVerificationOptions =
95
+ adRequestOptions["serverSideVerificationOptions"] as? Dictionary<String, Any> {
96
+ let options = GADServerSideVerificationOptions()
97
+
98
+ if let userId = serverSideVerificationOptions["userId"] as? String {
99
+ options.userIdentifier = userId
100
+ }
101
+ if let customData = serverSideVerificationOptions["customData"] as? String {
102
+ options.customRewardString = customData
103
+ }
104
+
105
+ if let ad = ad as? GADRewardedAd {
106
+ ad.serverSideVerificationOptions = options
107
+ }
108
+ else if let ad = ad as? GADRewardedInterstitialAd {
109
+ ad.serverSideVerificationOptions = options
110
+ }
111
+ }
112
+
113
+ eventType = GOOGLE_MOBILE_ADS_EVENT_REWARDED_LOADED
114
+
115
+ let adReward = (ad as? GADRewardedAd)?.adReward ?? (ad as? GADRewardedInterstitialAd)?.adReward
116
+ data = [
117
+ "type": adReward!.type,
118
+ "amount": adReward!.amount
119
+ ]
120
+ }
121
+
122
+ if let ad = ad as? GAMInterstitialAd {
123
+ ad.appEventDelegate = delegate
124
+ }
125
+
126
+ ad!.fullScreenContentDelegate = delegate
127
+
128
+ self.adMap.updateValue(ad!, forKey: requestId)
129
+ self.delegateMap.updateValue(delegate, forKey: requestId)
130
+ self.sendAdEvent(
131
+ eventType,
132
+ requestId: requestId,
133
+ adUnitId: adUnitId,
134
+ error: nil,
135
+ data: data
136
+ )
137
+ }
138
+
139
+ loadAd(adUnitId: adUnitId, adRequest: adRequest, completionHandler: completionHandler)
140
+ }
141
+
142
+ //MARK: - Show function
143
+
144
+ func show(
145
+ requestId: Int,
146
+ adUnitId: String,
147
+ showOptions: Dictionary<String, Any>,
148
+ resolve: RCTPromiseResolveBlock?,
149
+ reject: RCTPromiseRejectBlock?
150
+ ) {
151
+ guard let viewController = RNGoogleMobileAdsCommon.currentViewController() else {
152
+ RNSharedUtils.rejectPromise(userInfo: reject, userInfo: [
153
+ "code" : "nil-vc",
154
+ "message" : "Ad attempted to show but the current View Controller was nil.",
155
+ ])
156
+ return
157
+ }
158
+
159
+ guard let ad = adMap[requestId] else {
160
+ RNSharedUtils.rejectPromise(userInfo: reject, userInfo: [
161
+ "code" : "not-ready",
162
+ "message" : "Ad attempted to show but was not ready.",
163
+ ])
164
+ return
165
+ }
166
+
167
+ var rewardData: Dictionary<String, Any>? = nil
168
+ if let adReward =
169
+ (ad as? GADRewardedAd)?.adReward ??
170
+ (ad as? GADRewardedInterstitialAd)?.adReward {
171
+ rewardData = [
172
+ "type": adReward.type,
173
+ "amount": adReward.amount
174
+ ]
175
+ }
176
+ let userDidEarnRewardHandler = { () -> Void in
177
+ self.sendAdEvent(
178
+ GOOGLE_MOBILE_ADS_EVENT_REWARDED_EARNED_REWARD,
179
+ requestId: requestId,
180
+ adUnitId: adUnitId,
181
+ error: nil,
182
+ data: rewardData
183
+ )
184
+ }
185
+
186
+ // TODO: Find way to compact this area
187
+ if let ad = ad as? GADAppOpenAd {
188
+ ad.present(fromRootViewController: viewController)
189
+ }
190
+ else if let ad = ad as? GADInterstitialAd {
191
+ ad.present(fromRootViewController: viewController)
192
+ }
193
+ else if let ad = ad as? GADRewardedAd {
194
+ ad.present(
195
+ fromRootViewController: viewController,
196
+ userDidEarnRewardHandler: userDidEarnRewardHandler
197
+ )
198
+ }
199
+ else if let ad = ad as? GADRewardedInterstitialAd {
200
+ ad.present(
201
+ fromRootViewController: viewController,
202
+ userDidEarnRewardHandler: userDidEarnRewardHandler
203
+ )
204
+ }
205
+
206
+ resolve!(nil)
207
+ }
208
+ }
@@ -0,0 +1,72 @@
1
+ import Foundation
2
+ import GoogleMobileAds
3
+
4
+ class RNGoogleMobileAdsFullScreenContentDelegate: NSObject, GADFullScreenContentDelegate, GADAppEventDelegate {
5
+ let adEventName: String
6
+ let requestId: Int
7
+ let adUnitId: String
8
+
9
+ init(adEventName: String, requestId: Int, adUnitId: String) {
10
+ self.adEventName = adEventName
11
+ self.requestId = requestId
12
+ self.adUnitId = adUnitId
13
+ }
14
+
15
+ func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
16
+ sendAdEvent(
17
+ type: GOOGLE_MOBILE_ADS_EVENT_OPENED,
18
+ error: nil,
19
+ data: nil
20
+ )
21
+ }
22
+
23
+ func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
24
+ sendAdEvent(
25
+ type: GOOGLE_MOBILE_ADS_EVENT_ERROR,
26
+ error: RNGoogleMobileAdsCommon.getCodeAndMessage(fromAdError: error) as? Dictionary<String, Any>,
27
+ data: nil
28
+ )
29
+ }
30
+
31
+ func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
32
+ sendAdEvent(
33
+ type: GOOGLE_MOBILE_ADS_EVENT_CLOSED,
34
+ error: nil,
35
+ data: nil
36
+ )
37
+ }
38
+
39
+ func adDidRecordClick(_ ad: GADFullScreenPresentingAd) {
40
+ sendAdEvent(
41
+ type: GOOGLE_MOBILE_ADS_EVENT_CLICKED,
42
+ error: nil,
43
+ data: nil
44
+ )
45
+ }
46
+
47
+ func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) {
48
+ // Not implemented yet
49
+ }
50
+
51
+ func interstitialAd(_ interstitialAd: GADInterstitialAd, didReceiveAppEvent name: String, withInfo info: String?) {
52
+ sendAdEvent(
53
+ type: GOOGLE_MOBILE_ADS_EVENT_APP_EVENT,
54
+ error: nil,
55
+ data: [
56
+ "name": name,
57
+ "data": info ?? ""
58
+ ]
59
+ )
60
+ }
61
+
62
+ private func sendAdEvent(type: String, error: Dictionary<String, Any>?, data: Dictionary<String, Any>?) {
63
+ RNGoogleMobileAdsCommon.sendAdEvent(
64
+ adEventName,
65
+ requestId: requestId as NSNumber,
66
+ type: type,
67
+ adUnitId: adUnitId,
68
+ error: error,
69
+ data: data
70
+ )
71
+ }
72
+ }