Skip to content

Commit d65c8a6

Browse files
committed
refactor(ios): get rootViewController considering scenes
1 parent f59fc83 commit d65c8a6

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

ios/Plugin/Banner/BannerExecutor.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import Capacitor
33
import GoogleMobileAds
44

55
class BannerExecutor: NSObject, GADBannerViewDelegate {
6-
public weak var plugin: CAPPlugin?
6+
public weak var plugin: AdMob?
77
var bannerView: GADBannerView!
88

99
func showBanner(_ call: CAPPluginCall, _ request: GADRequest, _ adUnitID: String) {
10-
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
10+
if let rootViewController = plugin?.getRootVC() {
1111

1212
let adSize = call.getString("adSize") ?? "ADAPTIVE_BANNER"
1313
let adPosition = call.getString("position") ?? "BOTTOM_CENTER"
@@ -53,7 +53,7 @@ class BannerExecutor: NSObject, GADBannerViewDelegate {
5353
self.addBannerViewToView(self.bannerView, adPosition, adMargin)
5454
self.bannerView.translatesAutoresizingMaskIntoConstraints = false
5555
self.bannerView.adUnitID = adUnitID
56-
self.bannerView.rootViewController = UIApplication.shared.keyWindow?.rootViewController
56+
self.bannerView.rootViewController = plugin?.getRootVC()
5757

5858
self.bannerView.load(request)
5959
self.bannerView.delegate = self
@@ -64,7 +64,7 @@ class BannerExecutor: NSObject, GADBannerViewDelegate {
6464

6565
func hideBanner(_ call: CAPPluginCall) {
6666
DispatchQueue.main.async {
67-
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
67+
if let rootViewController = self.plugin?.getRootVC() {
6868
if let subView = rootViewController.view.viewWithTag(2743243288699) {
6969
NSLog("AdMob: find subView for hideBanner")
7070
subView.isHidden = true
@@ -83,7 +83,7 @@ class BannerExecutor: NSObject, GADBannerViewDelegate {
8383
}
8484

8585
func resumeBanner(_ call: CAPPluginCall) {
86-
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
86+
if let rootViewController = plugin?.getRootVC() {
8787
if let subView = rootViewController.view.viewWithTag(2743243288699) {
8888
NSLog("AdMob: find subView for resumeBanner")
8989
subView.isHidden = false
@@ -109,7 +109,7 @@ class BannerExecutor: NSObject, GADBannerViewDelegate {
109109

110110
private func addBannerViewToView(_ bannerView: GADBannerView, _ adPosition: String, _ Margin: Int) {
111111
removeBannerViewToView()
112-
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
112+
if let rootViewController = plugin?.getRootVC() {
113113

114114
bannerView.translatesAutoresizingMaskIntoConstraints = false
115115
bannerView.tag = 2743243288699 // rand
@@ -134,7 +134,7 @@ class BannerExecutor: NSObject, GADBannerViewDelegate {
134134
}
135135

136136
private func removeBannerViewToView() {
137-
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
137+
if let rootViewController = plugin?.getRootVC() {
138138
if let subView = rootViewController.view.viewWithTag(2743243288699) {
139139
bannerView.delegate = nil
140140
NSLog("AdMob: find subView")

ios/Plugin/Interstitial/AdInterstitialExecutor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Capacitor
33
import GoogleMobileAds
44

55
class AdInterstitialExecutor: NSObject, GADFullScreenContentDelegate {
6-
public weak var plugin: CAPPlugin?
6+
public weak var plugin: AdMob?
77
var interstitial: GADInterstitialAd!
88

99
func prepareInterstitial(_ call: CAPPluginCall, _ request: GADRequest, _ adUnitID: String) {
@@ -32,7 +32,7 @@ class AdInterstitialExecutor: NSObject, GADFullScreenContentDelegate {
3232
}
3333

3434
func showInterstitial(_ call: CAPPluginCall) {
35-
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
35+
if let rootViewController = plugin?.getRootVC() {
3636
if let ad = self.interstitial {
3737
ad.present(fromRootViewController: rootViewController)
3838
call.resolve([:])
@@ -50,7 +50,7 @@ class AdInterstitialExecutor: NSObject, GADFullScreenContentDelegate {
5050
"message": error.localizedDescription
5151
])
5252
}
53-
53+
5454
public func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
5555
NSLog("InterstitialFullScreenDelegate Ad did present full screen content.")
5656
self.plugin?.notifyListeners(InterstitialAdPluginEvents.Showed.rawValue, data: [:])

ios/Plugin/Plugin.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,17 @@ public class AdMob: CAPPlugin {
201201
}
202202

203203
}
204+
205+
func getRootVC() -> UIViewController? {
206+
var window: UIWindow? = UIApplication.shared.delegate?.window ?? nil
207+
208+
if window == nil {
209+
let scene: UIWindowScene? = UIApplication.shared.connectedScenes.first as? UIWindowScene
210+
window = scene?.windows.filter({$0.isKeyWindow}).first
211+
if window == nil {
212+
window = scene?.windows.first
213+
}
214+
}
215+
return window?.rootViewController
216+
}
204217
}

ios/Plugin/Rewarded/AdRewardExecutor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Capacitor
33
import GoogleMobileAds
44

55
class AdRewardExecutor: NSObject, GADFullScreenContentDelegate {
6-
public weak var plugin: CAPPlugin?
6+
public weak var plugin: AdMob?
77
var rewardedAd: GADRewardedAd!
88

99
func prepareRewardVideoAd(_ call: CAPPluginCall, _ request: GADRequest, _ adUnitID: String) {
@@ -49,7 +49,7 @@ class AdRewardExecutor: NSObject, GADFullScreenContentDelegate {
4949
}
5050

5151
func showRewardVideoAd(_ call: CAPPluginCall) {
52-
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
52+
if let rootViewController = plugin?.getRootVC() {
5353
if let ad = self.rewardedAd {
5454
ad.present(fromRootViewController: rootViewController,
5555
userDidEarnRewardHandler: {
@@ -71,7 +71,7 @@ class AdRewardExecutor: NSObject, GADFullScreenContentDelegate {
7171
"message": error.localizedDescription
7272
])
7373
}
74-
74+
7575
public func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
7676
NSLog("RewardFullScreenDelegate Ad did present full screen content.")
7777
self.plugin?.notifyListeners(RewardAdPluginEvents.Showed.rawValue, data: [:])

0 commit comments

Comments
 (0)