Banner Ads
Step 1: Request for Ads:
To request ads, use the following method:
func requestForBannerView(viewController: UIViewController?) -> UIView {
guard let bannerView = GADBannerManager().createRequestForBannerView(viewController: viewController) else {
return UIView() // Return an empty view if the banner creation fails
}
return bannerView // Return the bannerView directly as UIView
}
func createRequestForBannerView(viewController: UIViewController?) -> GADBannerView? {
bannerView = GADBannerView(adSize: GADAdSizeBanner)
bannerView?.adUnitID = getBannerAdUnitID()
bannerView?.rootViewController = viewController
bannerView?.delegate = self
bannerView?.load(GADRequest())
return bannerView
}
-
bannerView: Banner view where the ad will present and this object will user to add on super view.
-
viewController: where the banner will be visible.
Step 2: Delegate method:
GADBannerViewDelegate method to for request call backs.
extension GADBannerManager: GADBannerViewDelegate {
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
// Add banner to view and add constraints as above.
print("Banner loaded successfully")
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("Fail to receive ads",error)
print(error)
}
/// Tells the delegate that a full-screen view will be presented in response
/// to the user clicking on an ad.
func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
print("adViewWillPresentScreen")
}
/// Tells the delegate that the full-screen view will be dismissed.
func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
print("adViewWillDismissScreen")
}
/// Tells the delegate that the full-screen view has been dismissed.
func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
print("adViewDidDismissScreen")
}
/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
print("adViewWillLeaveApplication")
}
}
- Banner view delegate view callbacks methods which will handle request response