iAd Admob与单身中介
我正在尝试使用iAd和Admob后备。 感谢这篇文章,我已经拿出下面的代码。 我有几个视图控制器,我打算显示一些补充。 代码目前的作品。 我看到debugging消息,如果iAd失败Admbod节目。 但是我不确定,如果是正确的做法。因为我不知道为什么原创作者重新初始化委托,appID和做另一个请求。 如果你能帮助我,我将不胜感激。
在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 } }
我的自定义类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) } }
在我的ViewController.swift中 ,
覆盖func viewWillAppear(animated:Bool){
super.viewWillAppear(animated) if !isRegistered { Registration.sharedInstance.showAds(self, view: nil) }