繁体   English   中英

Monotouch.Dialog和iAds

[英]Monotouch.Dialog and iAds

我正在使用iAds。 我想将其添加到基本上是一堆Monotouch.Dialog视图的应用程序中。

是添加一个UIViewController,然后添加一个ADBannerViewMonotouch.Dialog它的最佳实践,或者我应该添加的iAds着眼于Monotouch.Dialog的ViewController?

我创建了一个效果很好的包装器ViewController,只需将主应用程序的视图(如TabBar或SplitView等)传递进来,然后将IADViewController用作RootViewCOntroller:

     public partial class IADViewController : UIViewController
{
    private UIViewController _anyVC;
    private MonoTouch.iAd.ADBannerView _ad;

    public IADViewController (UIViewController anyVC)
    {
        _anyVC = anyVC;
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        View.AddSubview (_anyVC.View);


        if (Common.Device.Is6AtLeast && Social.IsiAdCountry) {

            try {
                _ad = new MonoTouch.iAd.ADBannerView (MonoTouch.iAd.ADAdType.Banner);
                _ad.Hidden = true;
                _ad.FailedToReceiveAd += HandleFailedToReceiveAd;
                _ad.AdLoaded += HandleAdLoaded;
                View.BackgroundColor = UIColor.Clear;
                _anyVC.View.Frame = View.Bounds;
                View.AddSubview (_ad);
            } catch {
            }
        } else {
            Resize ();
        }
    }

    public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
    {
        base.DidRotate (fromInterfaceOrientation);
        Resize ();
    }

    public override void ViewDidAppear (bool animated)
    {
        base.ViewDidAppear (animated);
        Resize ();
    }

    void Resize ()
    {

        UIView.Animate (.25,
            () => {
                if (_ad !=null && _ad.Hidden == false) {
                    _anyVC.View.Frame = new RectangleF (0, 0, this.View.Bounds.Width, this.View.Bounds.Height - _ad.Frame.Height);
                } else {
                    _anyVC.View.Frame = View.Bounds;
                }
            });
        if(_ad!=null)
           _ad.Frame = new RectangleF (0, _anyVC.View.Bounds.Height, this.View.Bounds.Width, _ad.Frame.Height);
    }

    void HandleAdLoaded (object sender, EventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = false;
        Resize ();
    }

    void HandleFailedToReceiveAd (object sender, AdErrorEventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = true;
        Resize ();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM