Skip to content

Commit bd39ad9

Browse files
mars-lanalvaromb
authored andcommitted
Replace the deprecated event dispatching via RCTEventDispatcher with … (#66)
* Replace the deprecated event dispatching via RCTEventDispatcher with RCTBubblingEventBlocks for iOS. * Fix broken build. * Fix broken include due to bad merge. * Use RCTBubblingEventBlocks in RNAdMobInterstitial & RNAdMobRewarded as well. * Revert "Use RCTBubblingEventBlocks in RNAdMobInterstitial & RNAdMobRewarded as well." This reverts commit 3bec144. * Update RNDFPBannerView.h Added right include check
1 parent 7ba89a5 commit bd39ad9

File tree

6 files changed

+94
-103
lines changed

6 files changed

+94
-103
lines changed

ios/BannerView.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#if __has_include(<React/RCTEventDispatcher.h>)
2-
#import <React/RCTEventDispatcher.h>
2+
#import <React/RCTComponent.h>
33
#else
4-
#import "RCTEventDispatcher.h"
4+
#import "RCTComponent.h"
55
#endif
66

77
@import GoogleMobileAds;
@@ -14,7 +14,14 @@
1414
@property (nonatomic, copy) NSString *adUnitID;
1515
@property (nonatomic, copy) NSString *testDeviceID;
1616

17-
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
17+
@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
18+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidReceiveAd;
19+
@property (nonatomic, copy) RCTBubblingEventBlock onDidFailToReceiveAdWithError;
20+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillPresentScreen;
21+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillDismissScreen;
22+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidDismissScreen;
23+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillLeaveApplication;
24+
1825
- (GADAdSize)getAdSizeFromString:(NSString *)bannerSize;
1926
- (void)loadBanner;
2027

ios/BannerView.m

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,8 @@
1212

1313
@implementation BannerView {
1414
GADBannerView *_bannerView;
15-
RCTEventDispatcher *_eventDispatcher;
1615
}
1716

18-
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
19-
{
20-
if ((self = [super initWithFrame:CGRectZero])) {
21-
_eventDispatcher = eventDispatcher;
22-
}
23-
return self;
24-
}
25-
26-
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
27-
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder)
28-
2917
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
3018
{
3119
RCTLogError(@"AdMob Banner cannot have any subviews");
@@ -65,13 +53,12 @@ -(void)loadBanner
6553
GADAdSize size = [self getAdSizeFromString:_bannerSize];
6654
_bannerView = [[GADBannerView alloc] initWithAdSize:size];
6755
if(!CGRectEqualToRect(self.bounds, _bannerView.bounds)) {
68-
[_eventDispatcher
69-
sendInputEventWithName:@"onSizeChange"
70-
body:@{
71-
@"target": self.reactTag,
56+
if (self.onSizeChange) {
57+
self.onSizeChange(@{
7258
@"width": [NSNumber numberWithFloat: _bannerView.bounds.size.width],
7359
@"height": [NSNumber numberWithFloat: _bannerView.bounds.size.height]
74-
}];
60+
});
61+
}
7562
}
7663
_bannerView.delegate = self;
7764
_bannerView.adUnitID = _adUnitID;
@@ -132,49 +119,49 @@ -(void)layoutSubviews
132119
[self addSubview:_bannerView];
133120
}
134121

135-
- (void)removeFromSuperview
136-
{
137-
_eventDispatcher = nil;
138-
[super removeFromSuperview];
139-
}
140-
141122
/// Tells the delegate an ad request loaded an ad.
142-
- (void)adViewDidReceiveAd:(GADBannerView *)adView
143-
{
144-
[_eventDispatcher sendInputEventWithName:@"onAdViewDidReceiveAd" body:@{ @"target": self.reactTag }];
123+
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
124+
if (self.onAdViewDidReceiveAd) {
125+
self.onAdViewDidReceiveAd(@{});
126+
}
145127
}
146128

147129
/// Tells the delegate an ad request failed.
148130
- (void)adView:(GADBannerView *)adView
149-
didFailToReceiveAdWithError:(GADRequestError *)error
150-
{
151-
[_eventDispatcher sendInputEventWithName:@"onDidFailToReceiveAdWithError" body:@{ @"target": self.reactTag, @"error": [error localizedDescription] }];
131+
didFailToReceiveAdWithError:(GADRequestError *)error {
132+
if (self.onDidFailToReceiveAdWithError) {
133+
self.onDidFailToReceiveAdWithError(@{@"error": [error localizedDescription]});
134+
}
152135
}
153136

154137
/// Tells the delegate that a full screen view will be presented in response
155138
/// to the user clicking on an ad.
156-
- (void)adViewWillPresentScreen:(GADBannerView *)adView
157-
{
158-
[_eventDispatcher sendInputEventWithName:@"onAdViewWillPresentScreen" body:@{ @"target": self.reactTag }];
139+
- (void)adViewWillPresentScreen:(GADBannerView *)adView {
140+
if (self.onAdViewWillPresentScreen) {
141+
self.onAdViewWillPresentScreen(@{});
142+
}
159143
}
160144

161145
/// Tells the delegate that the full screen view will be dismissed.
162-
- (void)adViewWillDismissScreen:(GADBannerView *)adView
163-
{
164-
[_eventDispatcher sendInputEventWithName:@"onAdViewWillDismissScreen" body:@{ @"target": self.reactTag }];
146+
- (void)adViewWillDismissScreen:(GADBannerView *)adView {
147+
if (self.onAdViewWillDismissScreen) {
148+
self.onAdViewWillDismissScreen(@{});
149+
}
165150
}
166151

167152
/// Tells the delegate that the full screen view has been dismissed.
168-
- (void)adViewDidDismissScreen:(GADBannerView *)adView
169-
{
170-
[_eventDispatcher sendInputEventWithName:@"onAdViewDidDismissScreen" body:@{ @"target": self.reactTag }];
153+
- (void)adViewDidDismissScreen:(GADBannerView *)adView {
154+
if (self.onAdViewDidDismissScreen) {
155+
self.onAdViewDidDismissScreen(@{});
156+
}
171157
}
172158

173159
/// Tells the delegate that a user click will open another app (such as
174160
/// the App Store), backgrounding the current app.
175-
- (void)adViewWillLeaveApplication:(GADBannerView *)adView
176-
{
177-
[_eventDispatcher sendInputEventWithName:@"onAdViewWillLeaveApplication" body:@{ @"target": self.reactTag }];
161+
- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
162+
if (self.onAdViewWillLeaveApplication) {
163+
self.onAdViewWillLeaveApplication(@{});
164+
}
178165
}
179166

180167
@end

ios/RNAdMobDFPManager.m

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,7 @@ @implementation RNAdMobDFPManager
1515

1616
- (UIView *)view
1717
{
18-
return [[RNDFPBannerView alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
19-
}
20-
21-
- (NSArray *) customDirectEventTypes
22-
{
23-
return @[
24-
@"onSizeChange",
25-
@"onAdViewDidReceiveAd",
26-
@"onDidFailToReceiveAdWithError",
27-
@"onAdViewWillPresentScreen",
28-
@"onAdViewWillDismissScreen",
29-
@"onAdViewDidDismissScreen",
30-
@"onAdViewWillLeaveApplication",
31-
@"onAdmobDispatchAppEvent"
32-
];
18+
return [[RNDFPBannerView alloc] init];
3319
}
3420

3521
- (dispatch_queue_t)methodQueue
@@ -42,4 +28,13 @@ - (dispatch_queue_t)methodQueue
4228
RCT_EXPORT_VIEW_PROPERTY(adUnitID, NSString);
4329
RCT_EXPORT_VIEW_PROPERTY(testDeviceID, NSString);
4430

31+
RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
32+
RCT_EXPORT_VIEW_PROPERTY(onAdmobDispatchAppEvent, RCTBubblingEventBlock)
33+
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidReceiveAd, RCTBubblingEventBlock)
34+
RCT_EXPORT_VIEW_PROPERTY(onDidFailToReceiveAdWithError, RCTBubblingEventBlock)
35+
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillPresentScreen, RCTBubblingEventBlock)
36+
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillDismissScreen, RCTBubblingEventBlock)
37+
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidDismissScreen, RCTBubblingEventBlock)
38+
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillLeaveApplication, RCTBubblingEventBlock)
39+
4540
@end

ios/RNAdMobManager.m

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@ @implementation RNAdMobManager
1515

1616
- (UIView *)view
1717
{
18-
return [[BannerView alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
19-
}
20-
21-
- (NSArray *) customDirectEventTypes
22-
{
23-
return @[
24-
@"onSizeChange",
25-
@"onAdViewDidReceiveAd",
26-
@"onDidFailToReceiveAdWithError",
27-
@"onAdViewWillPresentScreen",
28-
@"onAdViewWillDismissScreen",
29-
@"onAdViewDidDismissScreen",
30-
@"onAdViewWillLeaveApplication"
31-
];
18+
return [[BannerView alloc] init];
3219
}
3320

3421
- (dispatch_queue_t)methodQueue
@@ -41,4 +28,12 @@ - (dispatch_queue_t)methodQueue
4128
RCT_EXPORT_VIEW_PROPERTY(adUnitID, NSString);
4229
RCT_EXPORT_VIEW_PROPERTY(testDeviceID, NSString);
4330

31+
RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
32+
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidReceiveAd, RCTBubblingEventBlock)
33+
RCT_EXPORT_VIEW_PROPERTY(onDidFailToReceiveAdWithError, RCTBubblingEventBlock)
34+
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillPresentScreen, RCTBubblingEventBlock)
35+
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillDismissScreen, RCTBubblingEventBlock)
36+
RCT_EXPORT_VIEW_PROPERTY(onAdViewDidDismissScreen, RCTBubblingEventBlock)
37+
RCT_EXPORT_VIEW_PROPERTY(onAdViewWillLeaveApplication, RCTBubblingEventBlock)
38+
4439
@end

ios/RNDFPBannerView.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#if __has_include(<React/RCTEventDispatcher.h>)
2-
#import <React/RCTEventDispatcher.h>
1+
#if __has_include(<React/RCTComponent.h>)
2+
#import <React/RCTComponent.h>
33
#else
4-
#import "RCTEventDispatcher.h"
4+
#import "RCTComponent.h"
55
#endif
66

77
@import GoogleMobileAds;
@@ -14,6 +14,15 @@
1414
@property (nonatomic, copy) NSString *adUnitID;
1515
@property (nonatomic, copy) NSString *testDeviceID;
1616

17+
@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
18+
@property (nonatomic, copy) RCTBubblingEventBlock onAdmobDispatchAppEvent;
19+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidReceiveAd;
20+
@property (nonatomic, copy) RCTBubblingEventBlock onDidFailToReceiveAdWithError;
21+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillPresentScreen;
22+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillDismissScreen;
23+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidDismissScreen;
24+
@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillLeaveApplication;
25+
1726
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
1827
- (GADAdSize)getAdSizeFromString:(NSString *)bannerSize;
1928
- (void)loadBanner;

ios/RNDFPBannerView.m

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,8 @@
1212

1313
@implementation RNDFPBannerView {
1414
DFPBannerView *_bannerView;
15-
RCTEventDispatcher *_eventDispatcher;
1615
}
1716

18-
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
19-
{
20-
if ((self = [super initWithFrame:CGRectZero])) {
21-
_eventDispatcher = eventDispatcher;
22-
}
23-
return self;
24-
}
25-
26-
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
27-
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder)
28-
2917
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
3018
{
3119
RCTLogError(@"AdMob Banner cannot have any subviews");
@@ -66,13 +54,12 @@ -(void)loadBanner {
6654
_bannerView = [[DFPBannerView alloc] initWithAdSize:size];
6755
[_bannerView setAppEventDelegate:self]; //added Admob event dispatch listener
6856
if(!CGRectEqualToRect(self.bounds, _bannerView.bounds)) {
69-
[_eventDispatcher
70-
sendInputEventWithName:@"onSizeChange"
71-
body:@{
72-
@"target": self.reactTag,
57+
if (self.onSizeChange) {
58+
self.onSizeChange(@{
7359
@"width": [NSNumber numberWithFloat: _bannerView.bounds.size.width],
7460
@"height": [NSNumber numberWithFloat: _bannerView.bounds.size.height]
75-
}];
61+
});
62+
}
7663
}
7764
_bannerView.delegate = self;
7865
_bannerView.adUnitID = _adUnitID;
@@ -97,7 +84,9 @@ - (void)adView:(DFPBannerView *)banner
9784
NSLog(@"Received app event (%@, %@)", name, info);
9885
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
9986
myDictionary[name] = info;
100-
[_eventDispatcher sendInputEventWithName:@"onAdmobDispatchAppEvent" body:@{ @"target": self.reactTag, name: info }];
87+
if (self.onAdmobDispatchAppEvent) {
88+
self.onAdmobDispatchAppEvent(@{ name: info });
89+
}
10190
}
10291

10392
- (void)setBannerSize:(NSString *)bannerSize
@@ -111,8 +100,6 @@ - (void)setBannerSize:(NSString *)bannerSize
111100
}
112101
}
113102

114-
115-
116103
- (void)setAdUnitID:(NSString *)adUnitID
117104
{
118105
if(![adUnitID isEqual:_adUnitID]) {
@@ -149,41 +136,52 @@ -(void)layoutSubviews
149136

150137
- (void)removeFromSuperview
151138
{
152-
_eventDispatcher = nil;
153139
[super removeFromSuperview];
154140
}
155141

156142
/// Tells the delegate an ad request loaded an ad.
157143
- (void)adViewDidReceiveAd:(DFPBannerView *)adView {
158-
[_eventDispatcher sendInputEventWithName:@"onAdViewDidReceiveAd" body:@{ @"target": self.reactTag }];
144+
if (self.onAdViewDidReceiveAd) {
145+
self.onAdViewDidReceiveAd(@{});
146+
}
159147
}
160148

161149
/// Tells the delegate an ad request failed.
162150
- (void)adView:(DFPBannerView *)adView
163151
didFailToReceiveAdWithError:(GADRequestError *)error {
164-
[_eventDispatcher sendInputEventWithName:@"onDidFailToReceiveAdWithError" body:@{ @"target": self.reactTag, @"error": [error localizedDescription] }];
152+
if (self.onDidFailToReceiveAdWithError) {
153+
self.onDidFailToReceiveAdWithError(@{ @"error": [error localizedDescription] });
154+
}
165155
}
166156

167157
/// Tells the delegate that a full screen view will be presented in response
168158
/// to the user clicking on an ad.
169159
- (void)adViewWillPresentScreen:(DFPBannerView *)adView {
170-
[_eventDispatcher sendInputEventWithName:@"onAdViewWillPresentScreen" body:@{ @"target": self.reactTag }];
160+
if (self.onAdViewWillPresentScreen) {
161+
self.onAdViewWillPresentScreen(@{});
162+
}
171163
}
172164

173165
/// Tells the delegate that the full screen view will be dismissed.
174166
- (void)adViewWillDismissScreen:(DFPBannerView *)adView {
175-
[_eventDispatcher sendInputEventWithName:@"onAdViewWillDismissScreen" body:@{ @"target": self.reactTag }];
167+
if (self.onAdViewWillDismissScreen) {
168+
self.onAdViewWillDismissScreen(@{});
169+
}
176170
}
177171

178172
/// Tells the delegate that the full screen view has been dismissed.
179173
- (void)adViewDidDismissScreen:(DFPBannerView *)adView {
180-
[_eventDispatcher sendInputEventWithName:@"onAdViewDidDismissScreen" body:@{ @"target": self.reactTag }];
174+
if (self.onAdViewDidDismissScreen) {
175+
self.onAdViewDidDismissScreen(@{});
176+
}
181177
}
182178

183179
/// Tells the delegate that a user click will open another app (such as
184180
/// the App Store), backgrounding the current app.
185181
- (void)adViewWillLeaveApplication:(DFPBannerView *)adView {
186-
[_eventDispatcher sendInputEventWithName:@"onAdViewWillLeaveApplication" body:@{ @"target": self.reactTag }];
182+
if (self.onAdViewWillLeaveApplication) {
183+
self.onAdViewWillLeaveApplication(@{});
184+
}
187185
}
188186

189187
@end

0 commit comments

Comments
 (0)