Google AdMob showing full-screen ad on the side

Viewed 17

I'm using the AdMob SDK to show ads in my app.

The problem is that on iPad the ad is on the side when iPad is on Landscape mode:

You can see that the ad is on the side when iPad is in Landscape mode.

I was wondering if this is a bug and if there is a workaround for it.

This is the code in my SceneDelegate:

import UIKit
import GoogleMobileAds

class SceneDelegate: UIResponder, UIWindowSceneDelegate, GADFullScreenContentDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        tryToPresentAd()
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }

    
    
    // ADS
    private var lastAdDate: Date?
    
    func requestAppOpenAd() {
        
        
        var appOpenAd: GADAppOpenAd? = nil
        let request = GADRequest()
        GADAppOpenAd.load(withAdUnitID: "ca-app-pub-3940256099942544/5662855259",
                          request: request,
                          orientation: UIInterfaceOrientation.portrait,
                          completionHandler: { [self] (appOpenAdIn, err) in
            
            // Ad loaded
            appOpenAd = appOpenAdIn
            appOpenAd?.fullScreenContentDelegate = self
            
            if let viewcontroller = adPresentationViewController() {
                DispatchQueue.main.async {
                    appOpenAd?.present(fromRootViewController: viewcontroller)
                }
            }
            
            self.lastAdDate = Date()
            print("Ad is ready")
            print(err as Any)
            
                          })
    }
    
    private func adPresentationViewController() -> UIViewController? {
        return window?.rootViewController
    }

    private func tryToPresentAd() {
        guard shouldShowFullScreenAd() else {
            return
        }
        
        self.requestAppOpenAd()
    }

    /// Returns false if 5 minutes did not pass since last ad.
    private func shouldShowFullScreenAd() -> Bool {
        let now = Date()
        guard let lastAdDate = lastAdDate else {
            return true
        }
        
        let time: TimeInterval = 5 * 60 // 5 minutes
        let shouldshow: Bool = now.timeIntervalSince(lastAdDate) >= time
        return shouldshow
    }
    
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        print("Advert error")
        print(error)
    }
    
    func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        print("adWillPresentFullScreenContent")
    }
}

0 Answers
Related