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
}
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")
}
}