Sergey Emeliyanov
Sergey Emeliyanov

Reputation: 6991

Getting landscape AdMob Interstitials while being in portrait orientation

The game app I'm currently developing manages its orientation with sensors and is always in portrait mode with this line of code inside onCreate() method:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

I've implemented AdMob Interstitial ads, however they seem to show up always in portrait mode, probably because the app thinks it is in portrait mode, since I'm managing screen rotations inside my game code.

Since I use sensors, I know precisely when in which orientation my device really is, is there a way I could request landscape ads from AdMob?

Upvotes: 0

Views: 1461

Answers (1)

S. Gissel
S. Gissel

Reputation: 2670

If the app still calls the viewWillTransition function it's quite easy.

Just override the function and use the createAndLoadInterstitial from the Interstitial Ads guideline from Google as normal.

Everytime the device will be rotated a new interstitial is requested. But it doesn't work when a interstitial is already displayed on screen.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        interstitial = createAndLoadInterstitial()
    }

func createAndLoadInterstitial() -> GADInterstitial {
        interstitial = GADInterstitial(adUnitID: GlobalConstants.interstitialAdUnitID)
        interstitial.delegate = self
        interstitial.load(GADRequest())
        return interstitial
    }

As a tip on the side. Introduce a class which dynamically switches AdUnitIDs between debug (test ads) and release (production ads):

class GlobalConstants {
    static var interstitialAdUnitID: String {
        #if DEBUG
        return "ca-app-pub-3940256099942544/4411468910"
        #else
        return "Production-AdUnitID"
        #endif
    }
}

Upvotes: 4

Related Questions