@@ -65,12 +65,25 @@ export function showBanner(arg: BannerOptions): Promise<any> {
6565
6666 firebase . admob . adView . loadRequest ( adRequest ) ;
6767
68- // TODO consider listening to delegate features like 'ad loaded' (Android resolves when the banner is actually showing)
69- // adView.delegate = self;
68+ // with interstitials you MUST wait for the ad to load before showing it, so requiring this delegate
69+ let delegate = GADBannerViewDelegateImpl . new ( ) . initWithOptionsAndCallback (
70+ arg ,
71+ ( ad : GADBannerView , error : GADRequestError ) => {
72+ if ( error ) {
73+ reject ( error . localizedDescription ) ;
74+ } else {
75+ resolve ( ) ;
76+ }
77+ } , ( ) => {
78+ arg . onClosed && arg . onClosed ( ) ;
79+ CFRelease ( delegate ) ;
80+ delegate = undefined ;
81+ } ) ;
7082
71- view . addSubview ( firebase . admob . adView ) ;
83+ CFRetain ( delegate ) ;
84+ firebase . admob . adView . delegate = delegate ;
7285
73- resolve ( ) ;
86+ view . addSubview ( firebase . admob . adView ) ;
7487 } catch ( ex ) {
7588 console . log ( "Error in firebase.admob.showBanner: " + ex ) ;
7689 reject ( ex ) ;
@@ -90,15 +103,17 @@ export function preloadInterstitial(arg: InterstitialOptions): Promise<any> {
90103 firebase . admob . interstitialView = GADInterstitial . alloc ( ) . initWithAdUnitID ( settings . iosInterstitialId ) ;
91104
92105 // with interstitials you MUST wait for the ad to load before showing it, so requiring this delegate
93- let delegate = GADInterstitialDelegateImpl . new ( ) . initWithCallback (
106+ let delegate = GADInterstitialDelegateImpl . new ( ) . initWithOptionsAndCallback (
107+ arg ,
94108 ( ad : GADInterstitial , error : GADRequestError ) => {
95109 if ( error ) {
96110 reject ( error . localizedDescription ) ;
97111 } else {
98112 resolve ( ) ;
99113 }
100114 } , ( ) => {
101- arg . onAdClosed && arg . onAdClosed ( ) ;
115+ arg . onAdClosed && arg . onAdClosed ( ) ; // TODO remove one day
116+ arg . onClosed && arg . onClosed ( ) ;
102117 CFRelease ( delegate ) ;
103118 delegate = undefined ;
104119 } ) ;
@@ -152,7 +167,7 @@ export function showInterstitial(arg?: InterstitialOptions): Promise<any> {
152167 firebase . admob . interstitialView = GADInterstitial . alloc ( ) . initWithAdUnitID ( settings . iosInterstitialId ) ;
153168
154169 // with interstitials you MUST wait for the ad to load before showing it, so requiring this delegate
155- let delegate = GADInterstitialDelegateImpl . new ( ) . initWithCallback ( ( ad : GADInterstitial , error : GADRequestError ) => {
170+ let delegate = GADInterstitialDelegateImpl . new ( ) . initWithOptionsAndCallback ( arg , ( ad : GADInterstitial , error : GADRequestError ) => {
156171 if ( error ) {
157172 reject ( error . localizedDescription ) ;
158173 } else {
@@ -331,7 +346,8 @@ function _getBannerType(size): any {
331346
332347class GADInterstitialDelegateImpl extends NSObject implements GADInterstitialDelegate {
333348 public static ObjCProtocols = [ ] ;
334- onAdCloseCallback : ( ) => void ;
349+ private options : InterstitialOptions ;
350+ private onAdCloseCallback : ( ) => void ;
335351
336352 static new ( ) : GADInterstitialDelegateImpl {
337353 if ( GADInterstitialDelegateImpl . ObjCProtocols . length === 0 && typeof ( GADInterstitialDelegate ) !== "undefined" ) {
@@ -342,7 +358,8 @@ class GADInterstitialDelegateImpl extends NSObject implements GADInterstitialDel
342358
343359 private callback : ( ad : GADInterstitial , error ?: GADRequestError ) => void ;
344360
345- public initWithCallback ( callback : ( ad : GADInterstitial , error ?: GADRequestError ) => void , onAdCloseCallback : ( ) => void = null ) : GADInterstitialDelegateImpl {
361+ public initWithOptionsAndCallback ( options : InterstitialOptions , callback : ( ad : GADInterstitial , error ?: GADRequestError ) => void , onAdCloseCallback : ( ) => void = null ) : GADInterstitialDelegateImpl {
362+ this . options = options ;
346363 this . callback = callback ;
347364 this . onAdCloseCallback = onAdCloseCallback ;
348365 return this ;
@@ -359,6 +376,48 @@ class GADInterstitialDelegateImpl extends NSObject implements GADInterstitialDel
359376 public interstitialDidFailToReceiveAdWithError ( ad : GADInterstitial , error : GADRequestError ) : void {
360377 this . callback ( ad , error ) ;
361378 }
379+
380+ public interstitialWillLeaveApplication ( ad : GADInterstitial ) : void {
381+ this . options . onLeftApplication && this . options . onLeftApplication ( ) ;
382+ }
383+ }
384+
385+ class GADBannerViewDelegateImpl extends NSObject implements GADBannerViewDelegate {
386+ public static ObjCProtocols = [ ] ;
387+ private onAdCloseCallback : ( ) => void ;
388+ private options : BannerOptions ;
389+
390+ static new ( ) : GADBannerViewDelegateImpl {
391+ if ( GADBannerViewDelegateImpl . ObjCProtocols . length === 0 && typeof ( GADBannerViewDelegate ) !== "undefined" ) {
392+ GADBannerViewDelegateImpl . ObjCProtocols . push ( GADBannerViewDelegate ) ;
393+ }
394+ return < GADBannerViewDelegateImpl > super . new ( ) ;
395+ }
396+
397+ private callback : ( ad : GADBannerView , error : GADRequestError ) => void ;
398+
399+ public initWithOptionsAndCallback ( options : BannerOptions , callback : ( ad : GADBannerView , error : GADRequestError ) => void , onAdCloseCallback : ( ) => void = null ) : GADBannerViewDelegateImpl {
400+ this . options = options ;
401+ this . callback = callback ;
402+ this . onAdCloseCallback = onAdCloseCallback ;
403+ return this ;
404+ }
405+
406+ public adViewDidReceiveAd ( bannerView : GADBannerView ) : void {
407+ this . callback ( bannerView , null ) ;
408+ }
409+
410+ public adViewDidFailToReceiveAdWithError ( bannerView : GADBannerView , error : GADRequestError ) : void {
411+ this . callback ( bannerView , error ) ;
412+ }
413+
414+ public adViewDidDismissScreen ( bannerView : GADBannerView ) : void {
415+ this . onAdCloseCallback ( ) ;
416+ }
417+
418+ public adViewWillLeaveApplication ( bannerView : GADBannerView ) : void {
419+ this . options . onLeftApplication && this . options . onLeftApplication ( ) ;
420+ }
362421}
363422
364423class GADRewardBasedVideoAdDelegateImpl extends NSObject implements GADRewardBasedVideoAdDelegate {
0 commit comments