adMob Smart Banner in Swift

2.6k Views Asked by At

Does anyone have an example of how to implement a Google mobile ads adMob Smart Banner that supports orientation changes in Swift?

I couldn't find it neither on StackOverflow nor on the Google documentation.

Thanks

4

There are 4 best solutions below

1
On

You can find the example code for Swift in GitHub.

GoogleAds-Mobile-iOS-Swift

1
On
0
On

There is an example of the Smart Banner implementation in the swift demos of AdMob APIDemo Just download the repository and try the demo. sample vertical sample horizontal

Note that it doesn't resize the banner when rotating the device. You still need to figure out that part.

0
On
  1. Follow the Official Quick start guide;
  2. With Autolayout, set the Ad view to your preferred size;
  3. in your code, before setting the adUnitID etc, set the adSize property to kGADAdSizeSmartBannerPortrait or kGADAdSizeSmartBannerLandscape;

Your new "finished example" code would be:

import UIKit
import GoogleMobileAds

class ViewController: UIViewController {

    @IBOutlet weak var bannerView: DFPBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        print("Google Mobile Ads SDK version: " + DFPRequest.sdkVersion())
        bannerView.adSize = kGADAdSizeSmartBannerLandscape
        bannerView.adUnitID = "/6499/example/banner"
        bannerView.rootViewController = self
        bannerView.loadRequest(DFPRequest())
    }
}