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