I am using AppDelegate to configure a banner view, and its delegates. Then displaying the banner on two VC's: ViewController, and SecondViewController. The banners show up properly, but with two visual mishaps.
On app open, there's about a 2-3 second delay for the test banner to load up. When I was using IB, the banners showed up instantly.
When transitioning from ViewController -> SecondViewController via segue (Present Modally / Over Full Screen / Cross Disolve / No Animation), there's a delay of about 1-2 seconds till the banner shows.
The banners load up perfectly, minus the two lag / delay issues above. How can I resolve these?
class AppDelegate: UIResponder, UIApplicationDelegate, GADBannerViewDelegate {
var window: UIWindow?
var adBannerView = GADBannerView()
let myBannerRequest = GADRequest()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
myBannerRequest.testDevices = [kGADSimulatorID]
adBannerView.delegate = self
adBannerView.isHidden = true
return true
}
func adViewDidReceiveAd(_ bannerView: GADBannerView!) {
adBannerView.isHidden = false
}
func adView(_ bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
adBannerView.isHidden = true
}
}
ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
requestAds()
}
internal func requestAds() {
appDelegate.adBannerView = GADBannerView()
appDelegate.adBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
appDelegate.adBannerView.adSize = kGADAdSizeBanner
appDelegate.adBannerView.rootViewController = self
appDelegate.adBannerView.load(appDelegate.myBannerRequest)
appDelegate.adBannerView.center = CGPoint(x: view.frame.midX, y: view.frame.height - appDelegate.adBannerView.frame.height / 2)
view.addSubview(appDelegate.adBannerView)
}
SecondViewController
// Same as ViewController, with addition of IBAction for the button:
@IBAction func closeButtonPressed(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
Looks like you're trying to create one GADBannerView
to use across your entire application, which is a good idea. You're not doing that though because you're creating a new GADBannerView
in your View Controller's requestAds
function: appDelegate.adBannerView = GADBannerView()
. Remove it.
Also, do your request and set your Ad Unit ID in your app delegate and remove it from your requestAds
function. In your View Controller's viewDidLoad
you should only be positioning your GADBannerView
and adding it to the view.
I imagine you would like the banner to fill the width of the screen too so I've changed the frame
property. If you wanted a function for this it would end up similar to:
func addBannerToView() {
appDelegate.adBannerView.adSize = kGADAdSizeBanner
appDelegate.adBannerView.rootViewController = self
appDelegate.adBannerView.frame = CGRect(x: 0.0,
y: view.frame.height - appDelegate.adBannerView.frame.height,
width: view.frame.width,
height: appDelegate.adBannerView.frame.height)
view.addSubview(appDelegate.adBannerView)
}