@@ -10,10 +10,10 @@ import GoogleMobileAds
1010 * here: https://capacitor.ionicframework.com/docs/plugins/ios
1111 */
1212@objc ( AdMob)
13- public class AdMob : CAPPlugin , GADBannerViewDelegate , GADFullScreenContentDelegate , GADRewardedAdDelegate , GADInterstitialDelegate {
13+ public class AdMob : CAPPlugin , GADBannerViewDelegate , GADFullScreenContentDelegate {
1414
1515 var bannerView : GADBannerView !
16- var interstitial : GADInterstitial !
16+ var interstitial : GADInterstitialAd !
1717 var rewardedAd : GADRewardedAd !
1818 var testingDevices : [ String ] = [ ]
1919
@@ -29,29 +29,29 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
2929
3030 if !isTrack {
3131 GADMobileAds . sharedInstance ( ) . start ( completionHandler: nil )
32- call. success ( [
32+ call. resolve ( [
3333 " value " : true
3434 ] )
3535 } else if #available( iOS 14 , * ) {
3636 #if canImport(AppTrackingTransparency)
3737 ATTrackingManager . requestTrackingAuthorization ( completionHandler: { _ in
3838 // iOS >= 14
3939 GADMobileAds . sharedInstance ( ) . start ( completionHandler: nil )
40- call. success ( [
40+ call. resolve ( [
4141 " value " : true
4242 ] )
4343
4444 } )
4545 #else
4646 GADMobileAds . sharedInstance ( ) . start ( completionHandler: nil )
47- call. success ( [
47+ call. resolve ( [
4848 " value " : true
4949 ] )
5050 #endif
5151 } else {
5252 // iOS < 14
5353 GADMobileAds . sharedInstance ( ) . start ( completionHandler: nil )
54- call. success ( [
54+ call. resolve ( [
5555 " value " : true
5656 ] )
5757 }
@@ -108,7 +108,7 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
108108 self . bannerView. load ( self . GADRequestWithOption ( call. getBool ( " npa " ) ?? false ) )
109109 self . bannerView. delegate = self
110110
111- call. success ( [
111+ call. resolve ( [
112112 " value " : true
113113 ] )
114114 }
@@ -130,7 +130,7 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
130130 " height " : 0
131131 ] )
132132
133- call. success ( [
133+ call. resolve ( [
134134 " value " : true
135135 ] )
136136 }
@@ -148,14 +148,14 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
148148 " height " : subView. frame. height
149149 ] )
150150
151- call. success ( [
151+ call. resolve ( [
152152 " value " : true
153153 ] )
154154
155155 } else {
156156 NSLog ( " AdMob: not find subView for resumeBanner " )
157157
158- call. success ( [
158+ call. resolve ( [
159159 " value " : false
160160 ] )
161161 }
@@ -166,7 +166,7 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
166166 @objc func removeBanner( _ call: CAPPluginCall ) {
167167 DispatchQueue . main. async {
168168 self . removeBannerViewToView ( )
169- call. success ( [
169+ call. resolve ( [
170170 " value " : true
171171 ] )
172172 }
@@ -283,11 +283,22 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
283283 adUnitID = testingID
284284 }
285285
286- self . interstitial = GADInterstitial ( adUnitID: adUnitID)
287286 let request = self . GADRequestWithOption ( call. getBool ( " npa " ) ?? false )
288- self . interstitial. load ( request)
289- self . interstitial. delegate = self
290- call. resolve ( [ " value " : true ] )
287+ GADInterstitialAd . load (
288+ withAdUnitID: adUnitID,
289+ request: request,
290+ completionHandler: { ( ad, error) in
291+ if error != nil {
292+ NSLog ( " Received error on loading interstatial ad " ) ;
293+ call. reject ( " Loading failed " )
294+ return ;
295+ }
296+
297+ self . interstitial = ad;
298+ self . interstitial. fullScreenContentDelegate = self ;
299+ call. resolve ( [ " value " : true ] )
300+ }
301+ )
291302 }
292303 }
293304
@@ -307,13 +318,13 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
307318
308319 // Intertitial Events Degigates
309320 /// Tells the delegate an ad request succeeded.
310- public func interstitialDidReceiveAd( _ ad: GADInterstitial ) {
321+ public func interstitialDidReceiveAd( _ ad: GADInterstitialAd ) {
311322 NSLog ( " interstitialDidReceiveAd " )
312323 self . notifyListeners ( " onInterstitialAdLoaded " , data: [ " value " : true ] )
313324 }
314325
315326 /// Tells the delegate an ad request failed.
316- public func interstitial( _ ad: GADInterstitial , didFailToReceiveAdWithError error: NSError ) {
327+ public func interstitial( _ ad: GADInterstitialAd , didFailToReceiveAdWithError error: NSError ) {
317328 NSLog ( " interstitial:didFailToReceiveAdWithError: \( error. localizedDescription) " )
318329 self . notifyListeners ( " onInterstitialAdFailedToLoad " , data: [ " error " : error. localizedDescription, " errorCode " : error. code] )
319330 }
@@ -346,32 +357,40 @@ public class AdMob: CAPPlugin, GADBannerViewDelegate, GADFullScreenContentDelega
346357 }
347358
348359 let request = self . GADRequestWithOption ( call. getBool ( " npa " ) ?? false )
349- self . rewardedAd = GADRewardedAd ( adUnitID: adUnitID)
350- self . rewardedAd? . load ( request) { error in
351- if let error = error {
352- // Handle ad failed to load case.
353- NSLog ( " Rewarded ad failed to load with error: \( error. localizedDescription) " )
354- call. reject ( " Loading failed " )
355- return
356- } else {
357- // Ad successfully loaded.
358- NSLog ( " AdMob Reward: Loading Succeeded " )
359- self . notifyListeners ( " onRewardedVideoAdLoaded " , data: [ " value " : true ] )
360- call. resolve ( [ " value " : true ] )
361- }
362- }
360+
361+ GADRewardedAd . load (
362+ withAdUnitID: adUnitID,
363+ request: request,
364+ completionHandler: { ( ad, error) in
365+ if let error = error {
366+ NSLog ( " Rewarded ad failed to load with error: \( error. localizedDescription) " )
367+ call. reject ( " Loading failed " )
368+ return
369+ }
370+
371+ NSLog ( " AdMob Reward: Loading Succeeded " )
372+
373+ self . notifyListeners ( " onRewardedVideoAdLoaded " , data: [ " value " : true ] )
374+ self . rewardedAd = ad
375+ self . rewardedAd? . fullScreenContentDelegate = self
376+ call. resolve ( [ " value " : true ] )
377+ }
378+ )
363379 }
364380 }
365381
366382 @objc func showRewardVideoAd( _ call: CAPPluginCall ) {
367383 DispatchQueue . main. async {
368384 if let rootViewController = UIApplication . shared. keyWindow? . rootViewController {
369- if self . rewardedAd? . isReady == true {
370- self . rewardedAd? . present ( fromRootViewController: rootViewController, delegate: self )
371- call. resolve ( [ " value " : true ] )
372- } else {
385+ if let ad = self . rewardedAd {
386+ ad. present ( fromRootViewController: rootViewController,
387+ userDidEarnRewardHandler: {
388+ call. resolve ( [ " value " : true ] )
389+ }
390+ )
391+ } else {
373392 call. reject ( " Reward Video is Not Ready Yet " )
374- }
393+ }
375394 }
376395
377396 }
0 commit comments