I am trying to use iAd with Admob fallback. Thanks this post, I have come up with following code. I have couple of view controllers that I am planning to show some adds. Code currently works. I see debug messages, if iAd fails Admbod shows. However I am not sure, if it is correct approach.Because I don't know why original author reinitialize delegate,appID and do another request. I will appreciate if you can help me on this.
In AppDelegate.swift
var iAdBanner = ADBannerView()
var adMobBanner = GADBannerView(adSize: kGADAdSizeBanner)
func bannerViewDidLoadAd(banner: ADBannerView!) {
UIView.beginAnimations(nil, context: nil)
iAdBanner.alpha = 1.0
adMobBanner.alpha = 0.0
UIView.commitAnimations()
NSLog("iAd loaded ad")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
UIView.beginAnimations(nil, context: nil)
iAdBanner.alpha = 0.0
adMobBanner.alpha = 1.0
UIView.commitAnimations()
NSLog("iAd failed to load ad")
}
func adView(view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
println("We dont have admob hide it!")
adMobBanner.alpha = 0
}
func adViewDidReceiveAd(view: GADBannerView!) {
if iAdBanner.alpha == 0.0 {
println("No IAD show admob")
adMobBanner.alpha = 1.0
}
}
My custom class Registration.swift
class Registration {
var appDelegate:AppDelegate!
static var sharedInstance = Registration()
func showAds(viewController:UIViewController, view:UIView?) {
var bannerView:UIView = view ?? viewController.view
//Admob
appDelegate.adMobBanner.rootViewController = viewController
appDelegate.adMobBanner.delegate = appDelegate
var request = GADRequest()
appDelegate.adMobBanner.adUnitID = "ca-id"
appDelegate.adMobBanner.loadRequest(request)
appDelegate.adMobBanner.setTranslatesAutoresizingMaskIntoConstraints(false)
bannerView.addSubview(appDelegate.adMobBanner)
var myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
bannerView.addConstraint(myConstraint)
myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
bannerView.addConstraint(myConstraint)
myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
bannerView.addConstraint(myConstraint)
// iAd
appDelegate.iAdBanner.delegate = appDelegate;
appDelegate.iAdBanner.setTranslatesAutoresizingMaskIntoConstraints(false)
bannerView.addSubview(appDelegate.iAdBanner)
// appDelegate.iAdBanner.alpha = 0.0
myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
bannerView.addConstraint(myConstraint)
myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
bannerView.addConstraint(myConstraint)
myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
bannerView.addConstraint(myConstraint)
}
}
In my ViewController.swift,
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if !isRegistered {
Registration.sharedInstance.showAds(self, view: nil)
}