Skip to content

Commit

Permalink
feat(ios): full screen ads on new arch
Browse files Browse the repository at this point in the history
  • Loading branch information
dylancom authored Dec 3, 2024
1 parent 757c7ef commit 4e1cf8f
Show file tree
Hide file tree
Showing 46 changed files with 1,143 additions and 951 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ When using The New Architecture, some legacy code will still be used though. See
| -------- | ------------------------------------------------- | ----------- |
| iOS | Mobile Ads SDK Methods (Turbo Native Module) | ✅ Complete |
| iOS | Banners (Fabric Native Component) | ✅ Complete |
| iOS | Full Screen Ads (Turbo Native Module) | ⏳ To-Do |
| iOS | Full Screen Ads (Turbo Native Module) | ✅ Complete |
| iOS | User Messaging Platform (Turbo Native Module) | ⏳ To-Do |
| iOS | EventEmitter (Turbo Native Module) | ⏳ To-Do |
| iOS | Revenue Precision Constants (Turbo Native Module) | ⏳ To-Do |
Expand Down
2 changes: 1 addition & 1 deletion __tests__/googleMobileAds.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import admob, { MaxAdContentRating } from '../src';
import RNGoogleMobileAdsModule from '../src/NativeGoogleMobileAdsModule';
import RNGoogleMobileAdsModule from '../src/specs/modules/NativeGoogleMobileAdsModule';

describe('Admob', function () {
describe('setRequestConfiguration()', function () {
Expand Down
32 changes: 9 additions & 23 deletions __tests__/interstitial.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AdEventType, InterstitialAd } from '../src';
import { NativeModules } from 'react-native';
import NativeInterstitialModule from '../src/specs/modules/NativeInterstitialModule';

describe('Google Mobile Ads Interstitial', function () {
describe('createForAdRequest', function () {
Expand Down Expand Up @@ -34,23 +34,17 @@ describe('Google Mobile Ads Interstitial', function () {
const ad = InterstitialAd.createForAdRequest('abc');

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).toBeCalledTimes(
1,
);
expect(NativeInterstitialModule.interstitialLoad).toBeCalledTimes(1);
});

it('does nothing if ad currently loading', () => {
const ad = InterstitialAd.createForAdRequest('abc');

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).toBeCalledTimes(
1,
);
expect(NativeInterstitialModule.interstitialLoad).toBeCalledTimes(1);

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).toBeCalledTimes(
1,
);
expect(NativeInterstitialModule.interstitialLoad).toBeCalledTimes(1);
});

it('does nothing if ad is already loaded', () => {
Expand All @@ -60,41 +54,33 @@ describe('Google Mobile Ads Interstitial', function () {
ad._handleAdEvent({ body: { type: AdEventType.LOADED } });

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).not.toBeCalled();
expect(NativeInterstitialModule.interstitialLoad).not.toBeCalled();
});

it('can be called again after ad was closed', () => {
const ad = InterstitialAd.createForAdRequest('abc');

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).toBeCalledTimes(
1,
);
expect(NativeInterstitialModule.interstitialLoad).toBeCalledTimes(1);

// @ts-ignore
ad._handleAdEvent({ body: { type: AdEventType.CLOSED } });

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).toBeCalledTimes(
2,
);
expect(NativeInterstitialModule.interstitialLoad).toBeCalledTimes(2);
});

it('can be called again after ad failed to load', () => {
const ad = InterstitialAd.createForAdRequest('abc');

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).toBeCalledTimes(
1,
);
expect(NativeInterstitialModule.interstitialLoad).toBeCalledTimes(1);

// @ts-ignore
ad._handleAdEvent({ body: { type: AdEventType.ERROR } });

ad.load();
expect(NativeModules.RNGoogleMobileAdsInterstitialModule.interstitialLoad).toBeCalledTimes(
2,
);
expect(NativeInterstitialModule.interstitialLoad).toBeCalledTimes(2);
});
});

Expand Down
36 changes: 36 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.
*
*/

#if !TARGET_OS_MACCATALYST

#import <Foundation/Foundation.h>

#ifdef RCT_NEW_ARCH_ENABLED

#import <RNGoogleMobileAdsSpec/RNGoogleMobileAdsSpec.h>
@interface RNGoogleMobileAdsAppOpenModule : NSObject <NativeAppOpenModuleSpec>

#else

#import <React/RCTBridgeModule.h>
@interface RNGoogleMobileAdsAppOpenModule : NSObject <RCTBridgeModule>

#endif

@end

#endif
46 changes: 0 additions & 46 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.m

This file was deleted.

100 changes: 100 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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.
*
*/

#if !TARGET_OS_MACCATALYST

#import "RNGoogleMobileAdsAppOpenModule.h"
#import <GoogleMobileAds/GoogleMobileAds.h>
#ifdef RCT_NEW_ARCH_ENABLED
#import "RNGoogleMobileAdsSpec.h"
#endif
#import "RNGoogleMobileAdsCommon.h"
#import "RNGoogleMobileAdsFullScreenAd.h"

@interface RNGoogleMobileAdsAppOpenAd : RNGoogleMobileAdsFullScreenAd

@end

@implementation RNGoogleMobileAdsAppOpenAd

- (NSString *)getAdEventName {
return GOOGLE_MOBILE_ADS_EVENT_APP_OPEN;
}

- (void)loadAd:(NSString *)adUnitId
adRequest:(GAMRequest *)adRequest
completionHandler:
(void (^)(GADAppOpenAd *_Nullable ad, NSError *_Nullable error))completionHandler {
[GADAppOpenAd loadWithAdUnitID:adUnitId request:adRequest completionHandler:completionHandler];
}

@end

@implementation RNGoogleMobileAdsAppOpenModule {
RNGoogleMobileAdsAppOpenAd *_ad;
}

RCT_EXPORT_MODULE();

- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}

RCT_EXPORT_METHOD(appOpenLoad
: (double)requestId adUnitId
: (NSString *)adUnitId requestOptions
: (NSDictionary *)requestOptions) {
[_ad loadWithRequestId:requestId adUnitId:adUnitId adRequestOptions:requestOptions];
}

RCT_EXPORT_METHOD(appOpenShow
: (double)requestId adUnitId
: (NSString *)adUnitId showOptions
: (NSDictionary *)showOptions resolve
: (RCTPromiseResolveBlock)resolve reject
: (RCTPromiseRejectBlock)reject) {
[_ad showWithRequestId:requestId
adUnitId:adUnitId
showOptions:showOptions
resolve:resolve
reject:reject];
}

RCT_EXPORT_METHOD(invalidate) { [_ad invalidate]; }

#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeAppOpenModuleSpecJSI>(params);
}
#endif

- (instancetype)init {
self = [super init];
if (self) {
_ad = [[RNGoogleMobileAdsAppOpenAd alloc] init];
}
return self;
}

- (void)dealloc {
[self invalidate];
}

@end

#endif
85 changes: 0 additions & 85 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.swift

This file was deleted.

4 changes: 4 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerView.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#if !TARGET_OS_MACCATALYST

// This guard prevent this file to be compiled in the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
#import <GoogleMobileAds/GADAppEventDelegate.h>
Expand Down Expand Up @@ -28,3 +30,5 @@ NS_ASSUME_NONNULL_END

#endif /* NativeComponentExampleComponentView_h */
#endif /* RCT_NEW_ARCH_ENABLED */

#endif
Loading

0 comments on commit 4e1cf8f

Please sign in to comment.