Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsMediaView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ @implementation RNGoogleMobileAdsMediaView {

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNGoogleMobileAdsBannerViewProps>();
static const auto defaultProps = std::make_shared<const RNGoogleMobileAdsMediaViewProps>();
_props = defaultProps;

_bridge = [RCTBridge currentBridge];
_nativeModule = [_bridge moduleForClass:RNGoogleMobileAdsNativeModule.class];
_mediaView = [[GADMediaView alloc] init];
_contentMode = UIViewContentModeScaleAspectFill;
self.contentView = _mediaView;
Expand Down Expand Up @@ -110,9 +108,17 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge {

#pragma mark - Common logics

- (RNGoogleMobileAdsNativeModule *)resolveNativeModule {
RCTBridge *bridge = [RCTBridge currentBridge];
if (bridge) {
return [bridge moduleForClass:RNGoogleMobileAdsNativeModule.class];
}
return [RNGoogleMobileAdsNativeModule sharedInstance];
}

- (void)setResponseId:(NSString *)responseId {
_responseId = responseId;
GADNativeAd *nativeAd = [_nativeModule nativeAdForResponseId:responseId];
GADNativeAd *nativeAd = [[self resolveNativeModule] nativeAdForResponseId:responseId];
_mediaView.mediaContent = nativeAd.mediaContent;
_mediaView.contentMode = _contentMode;
}
Expand Down
1 change: 1 addition & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@interface RNGoogleMobileAdsNativeModule : RCTEventEmitter <RCTBridgeModule>
#endif

+ (instancetype)sharedInstance;
- (GADNativeAd *)nativeAdForResponseId:(NSString *)responseId;

@end
7 changes: 7 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ - (dispatch_queue_t)methodQueue {
}
#endif

static __weak RNGoogleMobileAdsNativeModule *_sharedInstance = nil;

+ (instancetype)sharedInstance {
return _sharedInstance;
}

- (instancetype)init {
if (self = [super init]) {
_adHolders = [NSMutableDictionary dictionary];
_sharedInstance = self;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In scenarios with multiple React Native instances (e.g., multiple bridges or surfaces), this assignment will overwrite the _sharedInstance with the most recently initialized module. This can cause views in older instances to incorrectly reference the module from the new instance, potentially leading to state inconsistencies or missing ad references. While acceptable as a workaround for bridgeless mode, consider if a more robust resolution mechanism (like using the module registry) is needed for multi-instance support.

}
return self;
}
Expand Down
12 changes: 9 additions & 3 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ - (instancetype)initWithFrame:(CGRect)frame {
static const auto defaultProps = std::make_shared<const RNGoogleMobileAdsNativeViewProps>();
_props = defaultProps;

_bridge = [RCTBridge currentBridge];
_nativeModule = [_bridge moduleForClass:RNGoogleMobileAdsNativeModule.class];
_nativeAdView = [[GADNativeAdView alloc] init];
self.contentView = _nativeAdView;
}
Expand Down Expand Up @@ -137,9 +135,17 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge {

#pragma mark - Common logics

- (RNGoogleMobileAdsNativeModule *)resolveNativeModule {
RCTBridge *bridge = [RCTBridge currentBridge];
if (bridge) {
return [bridge moduleForClass:RNGoogleMobileAdsNativeModule.class];
}
return [RNGoogleMobileAdsNativeModule sharedInstance];
}

- (void)setResponseId:(NSString *)responseId {
_responseId = responseId;
_nativeAd = [_nativeModule nativeAdForResponseId:responseId];
_nativeAd = [[self resolveNativeModule] nativeAdForResponseId:responseId];
[self reloadAd];
}

Expand Down