iAd Admob与单身人士的调解

时间:2015-07-03 16:16:53

标签: ios swift admob iad

我正在尝试将iAd与Admob后备版一起使用。感谢this帖子,我提出了以下代码。我有几个视图控制器,我打算显示一些添加。代码目前有效。我看到调试消息,如果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(动画:Bool){

    super.viewWillAppear(animated)

    if !isRegistered {
        Registration.sharedInstance.showAds(self, view: nil)

}

0 个答案:

没有答案