diff --git a/ios/BannerView.h b/ios/BannerView.h index 3339bbe16..307580100 100644 --- a/ios/BannerView.h +++ b/ios/BannerView.h @@ -1,7 +1,7 @@ #if __has_include() -#import +#import #else -#import "RCTEventDispatcher.h" +#import "RCTComponent.h" #endif @import GoogleMobileAds; @@ -14,7 +14,14 @@ @property (nonatomic, copy) NSString *adUnitID; @property (nonatomic, copy) NSString *testDeviceID; -- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; +@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidReceiveAd; +@property (nonatomic, copy) RCTBubblingEventBlock onDidFailToReceiveAdWithError; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillPresentScreen; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillDismissScreen; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidDismissScreen; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillLeaveApplication; + - (GADAdSize)getAdSizeFromString:(NSString *)bannerSize; - (void)loadBanner; diff --git a/ios/BannerView.m b/ios/BannerView.m index 89997aba1..3c60f434b 100644 --- a/ios/BannerView.m +++ b/ios/BannerView.m @@ -12,20 +12,8 @@ @implementation BannerView { GADBannerView *_bannerView; - RCTEventDispatcher *_eventDispatcher; } -- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher -{ - if ((self = [super initWithFrame:CGRectZero])) { - _eventDispatcher = eventDispatcher; - } - return self; -} - -RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) -RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder) - - (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex { RCTLogError(@"AdMob Banner cannot have any subviews"); @@ -65,13 +53,12 @@ -(void)loadBanner GADAdSize size = [self getAdSizeFromString:_bannerSize]; _bannerView = [[GADBannerView alloc] initWithAdSize:size]; if(!CGRectEqualToRect(self.bounds, _bannerView.bounds)) { - [_eventDispatcher - sendInputEventWithName:@"onSizeChange" - body:@{ - @"target": self.reactTag, + if (self.onSizeChange) { + self.onSizeChange(@{ @"width": [NSNumber numberWithFloat: _bannerView.bounds.size.width], @"height": [NSNumber numberWithFloat: _bannerView.bounds.size.height] - }]; + }); + } } _bannerView.delegate = self; _bannerView.adUnitID = _adUnitID; @@ -132,49 +119,49 @@ -(void)layoutSubviews [self addSubview:_bannerView]; } -- (void)removeFromSuperview -{ - _eventDispatcher = nil; - [super removeFromSuperview]; -} - /// Tells the delegate an ad request loaded an ad. -- (void)adViewDidReceiveAd:(GADBannerView *)adView -{ - [_eventDispatcher sendInputEventWithName:@"onAdViewDidReceiveAd" body:@{ @"target": self.reactTag }]; +- (void)adViewDidReceiveAd:(GADBannerView *)adView { + if (self.onAdViewDidReceiveAd) { + self.onAdViewDidReceiveAd(@{}); + } } /// Tells the delegate an ad request failed. - (void)adView:(GADBannerView *)adView -didFailToReceiveAdWithError:(GADRequestError *)error -{ - [_eventDispatcher sendInputEventWithName:@"onDidFailToReceiveAdWithError" body:@{ @"target": self.reactTag, @"error": [error localizedDescription] }]; +didFailToReceiveAdWithError:(GADRequestError *)error { + if (self.onDidFailToReceiveAdWithError) { + self.onDidFailToReceiveAdWithError(@{@"error": [error localizedDescription]}); + } } /// Tells the delegate that a full screen view will be presented in response /// to the user clicking on an ad. -- (void)adViewWillPresentScreen:(GADBannerView *)adView -{ - [_eventDispatcher sendInputEventWithName:@"onAdViewWillPresentScreen" body:@{ @"target": self.reactTag }]; +- (void)adViewWillPresentScreen:(GADBannerView *)adView { + if (self.onAdViewWillPresentScreen) { + self.onAdViewWillPresentScreen(@{}); + } } /// Tells the delegate that the full screen view will be dismissed. -- (void)adViewWillDismissScreen:(GADBannerView *)adView -{ - [_eventDispatcher sendInputEventWithName:@"onAdViewWillDismissScreen" body:@{ @"target": self.reactTag }]; +- (void)adViewWillDismissScreen:(GADBannerView *)adView { + if (self.onAdViewWillDismissScreen) { + self.onAdViewWillDismissScreen(@{}); + } } /// Tells the delegate that the full screen view has been dismissed. -- (void)adViewDidDismissScreen:(GADBannerView *)adView -{ - [_eventDispatcher sendInputEventWithName:@"onAdViewDidDismissScreen" body:@{ @"target": self.reactTag }]; +- (void)adViewDidDismissScreen:(GADBannerView *)adView { + if (self.onAdViewDidDismissScreen) { + self.onAdViewDidDismissScreen(@{}); + } } /// Tells the delegate that a user click will open another app (such as /// the App Store), backgrounding the current app. -- (void)adViewWillLeaveApplication:(GADBannerView *)adView -{ - [_eventDispatcher sendInputEventWithName:@"onAdViewWillLeaveApplication" body:@{ @"target": self.reactTag }]; +- (void)adViewWillLeaveApplication:(GADBannerView *)adView { + if (self.onAdViewWillLeaveApplication) { + self.onAdViewWillLeaveApplication(@{}); + } } @end diff --git a/ios/RNAdMobDFPManager.m b/ios/RNAdMobDFPManager.m index c30b41c14..e48427eaf 100644 --- a/ios/RNAdMobDFPManager.m +++ b/ios/RNAdMobDFPManager.m @@ -15,21 +15,7 @@ @implementation RNAdMobDFPManager - (UIView *)view { - return [[RNDFPBannerView alloc] initWithEventDispatcher:self.bridge.eventDispatcher]; -} - -- (NSArray *) customDirectEventTypes -{ - return @[ - @"onSizeChange", - @"onAdViewDidReceiveAd", - @"onDidFailToReceiveAdWithError", - @"onAdViewWillPresentScreen", - @"onAdViewWillDismissScreen", - @"onAdViewDidDismissScreen", - @"onAdViewWillLeaveApplication", - @"onAdmobDispatchAppEvent" - ]; + return [[RNDFPBannerView alloc] init]; } - (dispatch_queue_t)methodQueue @@ -42,4 +28,13 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_VIEW_PROPERTY(adUnitID, NSString); RCT_EXPORT_VIEW_PROPERTY(testDeviceID, NSString); +RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdmobDispatchAppEvent, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewDidReceiveAd, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onDidFailToReceiveAdWithError, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewWillPresentScreen, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewWillDismissScreen, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewDidDismissScreen, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewWillLeaveApplication, RCTBubblingEventBlock) + @end diff --git a/ios/RNAdMobManager.m b/ios/RNAdMobManager.m index 59a1fb240..86a4bd463 100644 --- a/ios/RNAdMobManager.m +++ b/ios/RNAdMobManager.m @@ -15,20 +15,7 @@ @implementation RNAdMobManager - (UIView *)view { - return [[BannerView alloc] initWithEventDispatcher:self.bridge.eventDispatcher]; -} - -- (NSArray *) customDirectEventTypes -{ - return @[ - @"onSizeChange", - @"onAdViewDidReceiveAd", - @"onDidFailToReceiveAdWithError", - @"onAdViewWillPresentScreen", - @"onAdViewWillDismissScreen", - @"onAdViewDidDismissScreen", - @"onAdViewWillLeaveApplication" - ]; + return [[BannerView alloc] init]; } - (dispatch_queue_t)methodQueue @@ -41,4 +28,12 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_VIEW_PROPERTY(adUnitID, NSString); RCT_EXPORT_VIEW_PROPERTY(testDeviceID, NSString); +RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewDidReceiveAd, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onDidFailToReceiveAdWithError, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewWillPresentScreen, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewWillDismissScreen, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewDidDismissScreen, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAdViewWillLeaveApplication, RCTBubblingEventBlock) + @end diff --git a/ios/RNDFPBannerView.h b/ios/RNDFPBannerView.h index 6e1544d7b..5d2543f90 100644 --- a/ios/RNDFPBannerView.h +++ b/ios/RNDFPBannerView.h @@ -1,7 +1,7 @@ -#if __has_include() -#import +#if __has_include() +#import #else -#import "RCTEventDispatcher.h" +#import "RCTComponent.h" #endif @import GoogleMobileAds; @@ -14,6 +14,15 @@ @property (nonatomic, copy) NSString *adUnitID; @property (nonatomic, copy) NSString *testDeviceID; +@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange; +@property (nonatomic, copy) RCTBubblingEventBlock onAdmobDispatchAppEvent; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidReceiveAd; +@property (nonatomic, copy) RCTBubblingEventBlock onDidFailToReceiveAdWithError; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillPresentScreen; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillDismissScreen; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewDidDismissScreen; +@property (nonatomic, copy) RCTBubblingEventBlock onAdViewWillLeaveApplication; + - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; - (GADAdSize)getAdSizeFromString:(NSString *)bannerSize; - (void)loadBanner; diff --git a/ios/RNDFPBannerView.m b/ios/RNDFPBannerView.m index 99dc9a72b..7446c608b 100644 --- a/ios/RNDFPBannerView.m +++ b/ios/RNDFPBannerView.m @@ -12,20 +12,8 @@ @implementation RNDFPBannerView { DFPBannerView *_bannerView; - RCTEventDispatcher *_eventDispatcher; } -- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher -{ - if ((self = [super initWithFrame:CGRectZero])) { - _eventDispatcher = eventDispatcher; - } - return self; -} - -RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) -RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder) - - (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex { RCTLogError(@"AdMob Banner cannot have any subviews"); @@ -66,13 +54,12 @@ -(void)loadBanner { _bannerView = [[DFPBannerView alloc] initWithAdSize:size]; [_bannerView setAppEventDelegate:self]; //added Admob event dispatch listener if(!CGRectEqualToRect(self.bounds, _bannerView.bounds)) { - [_eventDispatcher - sendInputEventWithName:@"onSizeChange" - body:@{ - @"target": self.reactTag, + if (self.onSizeChange) { + self.onSizeChange(@{ @"width": [NSNumber numberWithFloat: _bannerView.bounds.size.width], @"height": [NSNumber numberWithFloat: _bannerView.bounds.size.height] - }]; + }); + } } _bannerView.delegate = self; _bannerView.adUnitID = _adUnitID; @@ -97,7 +84,9 @@ - (void)adView:(DFPBannerView *)banner NSLog(@"Received app event (%@, %@)", name, info); NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init]; myDictionary[name] = info; - [_eventDispatcher sendInputEventWithName:@"onAdmobDispatchAppEvent" body:@{ @"target": self.reactTag, name: info }]; + if (self.onAdmobDispatchAppEvent) { + self.onAdmobDispatchAppEvent(@{ name: info }); + } } - (void)setBannerSize:(NSString *)bannerSize @@ -111,8 +100,6 @@ - (void)setBannerSize:(NSString *)bannerSize } } - - - (void)setAdUnitID:(NSString *)adUnitID { if(![adUnitID isEqual:_adUnitID]) { @@ -149,41 +136,52 @@ -(void)layoutSubviews - (void)removeFromSuperview { - _eventDispatcher = nil; [super removeFromSuperview]; } /// Tells the delegate an ad request loaded an ad. - (void)adViewDidReceiveAd:(DFPBannerView *)adView { - [_eventDispatcher sendInputEventWithName:@"onAdViewDidReceiveAd" body:@{ @"target": self.reactTag }]; + if (self.onAdViewDidReceiveAd) { + self.onAdViewDidReceiveAd(@{}); + } } /// Tells the delegate an ad request failed. - (void)adView:(DFPBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error { - [_eventDispatcher sendInputEventWithName:@"onDidFailToReceiveAdWithError" body:@{ @"target": self.reactTag, @"error": [error localizedDescription] }]; + if (self.onDidFailToReceiveAdWithError) { + self.onDidFailToReceiveAdWithError(@{ @"error": [error localizedDescription] }); + } } /// Tells the delegate that a full screen view will be presented in response /// to the user clicking on an ad. - (void)adViewWillPresentScreen:(DFPBannerView *)adView { - [_eventDispatcher sendInputEventWithName:@"onAdViewWillPresentScreen" body:@{ @"target": self.reactTag }]; + if (self.onAdViewWillPresentScreen) { + self.onAdViewWillPresentScreen(@{}); + } } /// Tells the delegate that the full screen view will be dismissed. - (void)adViewWillDismissScreen:(DFPBannerView *)adView { - [_eventDispatcher sendInputEventWithName:@"onAdViewWillDismissScreen" body:@{ @"target": self.reactTag }]; + if (self.onAdViewWillDismissScreen) { + self.onAdViewWillDismissScreen(@{}); + } } /// Tells the delegate that the full screen view has been dismissed. - (void)adViewDidDismissScreen:(DFPBannerView *)adView { - [_eventDispatcher sendInputEventWithName:@"onAdViewDidDismissScreen" body:@{ @"target": self.reactTag }]; + if (self.onAdViewDidDismissScreen) { + self.onAdViewDidDismissScreen(@{}); + } } /// Tells the delegate that a user click will open another app (such as /// the App Store), backgrounding the current app. - (void)adViewWillLeaveApplication:(DFPBannerView *)adView { - [_eventDispatcher sendInputEventWithName:@"onAdViewWillLeaveApplication" body:@{ @"target": self.reactTag }]; + if (self.onAdViewWillLeaveApplication) { + self.onAdViewWillLeaveApplication(@{}); + } } @end